Share
Embed
techConnect 2012: Intro to Java Programming - Day 2
Slides for techConnect 2012's Intro to Java Programming series, Day 2. The tenets of Object Oriented Programming are introduced.
Doug Stephen
July 02, 2012
Transcript
27 slides
-
Introduction to Java Programming 2 July 2012: Day 2 John
Taylor Doug Stephen -
Object Oriented Programming • Model programs as “nouns” and “verbs”
• Nouns -> Objects • Verbs -> Methods -
How do we use objects? • We “define” an object
using an idea called a Class • Classes are an extension of Types • The types we looked at last time are called Primitive Types • Classes are known as Composite Types -
The Pieces of a Composite Type • A Class is
the “definition” for some type of Object • When you actually want to use an Object that is of some Class type, you instantiate an instance of that class. -
The Pieces of a Composite Type • Classes are made
up of fields and methods • Fields are variables • Methods are methods like we saw last time -
Fields • There are two main types of fields •
Instance fields are variables that belong to each instance of a Class • Class fields are fields that are shared by all instances of a Class -
Fields - Access Qualifiers • Fields aren’t always directly assigned
or manipulated like regular variables • We usually affect change in an Object by using methods instead • We restrict access to fields using Access Qualifiers: • Public, Private, Protected -
What Does this Look Like? class RocketShip{ // private instance
fields private double speed; private double fuel; private boolean isDamaged; // public instance field public String name; // Class field public static int fleetNumber; -
What Does this Look Like? spaceShuttle; // spaceShuttle is an
Instance of RocketShip // accessing public instance fields System.out.println(spaceShuttle.name); // accessing public Class fields System.out.println(“This shuttle is part of fleet number “ + RocketShip.fleetNumber); -
What Does this Look Like? /* Breaking down System.out.println */
System.out.println(someString); Class (Not Object!) Method sent to “out” object -
How about Methods? • Methods work very similarly to fields:
• Methods have access qualifiers • Methods can be instance methods or Class methods -
Why Methods? • We’ve already seen how reusability is convenient
• We want to “protect” data • Instead of setting “gallonsOfFuel” directly, we will call a method “refuel()” that makes sure the value isn’t set without actually putting fuel in -
Inheritance • Classes can be subclasses of other classes! •
You may have a class called Animal. You may have another class called Dog, which can subclass Animal -
Inheritance • Subclasses inhertit all of the public and protected
methods and fields from their parent classes • Inherited methods can be overloaded (rewritten) to be specific to the subclass. But the method can still have the same name and Java will magically know which version of the method to use! -
THE FOUR PRINCIPLES OF OOP • Abstraction • Think at
the high level instead of at the low level (Objects and Methods) • Inheritance • Design program to leverage inheritance so you don’t repeat yourself • Encapsulation • Group data in to Classes, protect data with access qualifiers and methods • Polymorphism • Method overloading, etc. -
Abstraction • Instead of worrying about a bunch of doubles
and Strings and how they can represent a RocketShip, we just make a RocketShip class and send it messages (methods) -
Inheritance • If you have classes for Dogs, Cats, Sheep,
and Mice, use Inheritance to move all of their shared traits in to a parent class (Animal, or Mammal, or FourLeggedMammal) -
Encapsulation • All of the related data should be grouped
together in to a class • Similarly, Classes shouldn’t have data that is unrelated to their behavior in them • Use access qualifiers and methods -
Encapsulation • Example: We want to know how much Fuel
our rocket has left in it, but not add to it unless we have actually fueled it up • We create an “accessor” method, called getFuel(), that returns how much fuel is remaining. • getFuel() is public, but the fuel field is private -
Polymorphism • RocketShip has the public method goHome() that sends
the rocket ship back to Earth • MartianRocketShip subclasses RocketShip, and so it inherits goHome(). But a MartianRocketShip goes back to Mars. • MartianRocketShip is a RocketShip and a MartianRocketShip but Java knows to use the goHome() that goes to Mars. This is polymorphism. -
Instantiation • Instantiation is done with a special type of
method called a Constructor // Default Constructor public RocketShip() { } // Other Constructors public RocketShip(double fuel) { this.fuel = fuel; } -
Instantiation • this is a special variable that refers to
the current instance object. You can only use this inside of instance methods (not inside of Class methods). -
An Example Class class RocketShip{ private double speed; private double
fuel; public RocketShip(double startingFuel) { fuel = startingFuel; } public double getSpeed() { return speed; } public double getFuel() { return fuel; } public void refuel(double fuel) { this.fuel += fuel; } } -
Variable Scope • Why do we use “this” sometimes? •
Variables have “scope”; they are alive as long as their block is alive (block -> curly braces) • Fields are alive as long as their class is alive because that is where they are declared -
Variable Scope • It is good practice when making constructors
or setters to use an argument name the same as the field name • Argument is only alive for the duration of the method execution • Use “this” to differentiate between the field and the argument -
Local Variables • Sometimes it’s helpful to declare variables inside
of a method. These variables are only alive while the method is executing (this is their scope) • We call these local variables -
Local Variables public int doSomething(int argument1) { int temp =
argument1 + 10; return temp; } Local Variable