Inheritance Notes: Using Object Oriented Design When Classes start a family…
HAS-A vs. IS-A Relationships Average Free Response Question The StudentRecord Class HAS-A array Money class Money object HAS-A couple of ints
HAS-A vs. IS-A Relationships New: IS-A Relationship Car IS-A Vehicle Triangle IS-A Polygon ProperFraction IS-A Fraction Many more to come… Car Vehicle Truck Corvette
Inheritance vocabulary Give an example of a polygon: Parent class – child class – grandchild class Super class – class - subclass Polygon Quadrilateral Triangle Rectangle
The Daddy of them all: Object All classes have the IS-A relationship with Object Inherited methods: toString() equals() hashCode() Many more that are not in the AP Subset Fraction Object Money ProperFraction
Creating a child class: extends public class Car extends Vehicle Car is the subclass It inherits all of Vehicle’s methods and variables It can “overwrite” the methods by using the same signature Ex: speedUp() method Car Vehicle Truck Corvette
A new keyword: super
Call Daddy’s methods: super Create a new Vehicle: Vehicle v1 = new Vehicle(); Create a new Car: Car c1 = new Car(); Polymorphism: If Vehicle and Car both have a speedUp() method, how do I call Vehicle’s speedUp () in Car’s methods? super.speedUp() Car Vehicle Truck Corvette speedUp() speedUp()
Call Daddy’s methods: super public class Vehicle{ private int speed; public void speedUp(){ speed++; S.O.P(“I’m fast”); } What does this do? In the runner: c1.speedUp(); public class Car extends Vehicle{ public void speedUp(){ S.O.P(“I’m really fast!”); super.speedUp(); } Car Vehicle Truck Corvette
Call Daddy’s constructor: super How does Car call Vehicle’s constructor? This is confusing because Vehicle’s constructor method is actually named _________________. super(parameter1, parameter2, …) Vehicle() Car Vehicle Truck Corvette
Never create me: abstract class abstract public class Polygon An object of the Vehicle class can never be instantiated (created) However, Vehicle can have child classes that CAN be instantiated Polygon Rectangle Triangle Square
Please create me: abstract method abstract double getArea(); An abstract method forces the creator of a child class to write the body of the method before the class can be compiled. Polygon Rectangle Triangle Square
Rules about constructors The superclass constructor can be called with the line super(param1, …) Daddy comes FIRST!!! The super constructor call MUST be the first line in the child constructor Car Vehicle Truck Corvette Car c1 = new Car();
More rules about constructors EMPHASIZE THIS! The magic of Java constructors!!! Daddy, help me!!!! Even if the word super is not present on the first line of the constructor, the child class constructor automatically calls its parent constructor before it does anything else. See sample questions…
Sample Question Answer? class Morf { private int numMorfs; public Morf(int m) {numMorfs = m;} public int getMorfs() {return numMorfs;} } class Dorf extends Morf private int numDorfs; public Dorf(int m, int d) {super(m); numDorfs = d;} public int getDorfs() {return numDorfs;} What will display when the following lines execute in a client file? Dorf dorf = new Dorf(10, 25); System.out.println("Morf count: " + dorf.getMorfs()); System.out.println("Dorf count: " + dorf.getDorfs()); Answer? See files MorfDorfQuestion.java and MorfDorfQuestion2.java
Another Sample Question class Morf { private int numMorfs; public Morf(int m) {numMorfs = m;} public int getMorfs() {return numMorfs}; public String toString(){ return “Morfs: “ + numMorfs;} } class Dorf extends Morf private int numDorfs; public Dorf(int m, int d) {super(m); numDorfs = d;} public int getDorfs() {return numDorfs;} What will display when the following lines execute in a client file? Dorf dorf = new Dorf(10, 25); System.out.println(dorf.toString()); System.out.println(dorf); Answer? See files MorfDorfQuestion.java and MorfDorfQuestion2.java
Don’t touch me: private variables private int myNum Private variables are only available to the class itself. They are NOT accessible to subclasses or client files. How are parent class private variables accessed?
Don’t touch me: private methods private void simplify() private methods can only be called within the class, not by children or clients.
Only family can touch me: protected methods protected int reproduce() Like private-Lite Only the class and child classes can call protected methods
Finally Let’s look at all of these topics in the following three files: Polygons.java PolygonsRunner.java Triangle.java Copy the files, paste them to your APCode folder, and add them to your workspace for a day of fun.
Question 1 Explanations: Answer? a.) didn’t implement moveBy method public abstract class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public String toString() { return "(" + getX + "," + getY() + ")"; } public abstract Point moveBy(int dx, int dy); public abstract double distanceFrom(Point p); } public abstract class SpecialPoint extends Point { public SpecialPoint(int x, int y) { < Code not shown > } public double distanceFrom(Point p) { < Code not shown > } } public class SpecialerPoint extends SpecialPoint { < Code not shown > } Which of the following is the minimal set of public constructors and/or methods required in the SpecialerPoint class, so that the statements SpecialerPoint pos = new SpecialerPoint(0, 0); System.out.println(pos.moveBy(10, 10).distanceFrom(pos)); compile with no errors? a. public Point moveBy(int dx, int dy) b. public SpecialerPoint(int x, int y) public Point moveBy(int dx, int dy) c. public double distanceFrom(Point pos) d. public double distanceFrom(SpecialerPoint pos) public SpecialerPoint moveBy(int dx, int dy) e. public SpecialerPoint() public SpecialerPoint(int x, int y) Explanations: a.) didn’t implement moveBy method b.) implements moveBy method (other abstract method implemented in SpecialPoint) c.) no constructor d.) no constructor e.) could be correct —> not minimum set of constructors/methods Question from Susan Ke
Unused Slides
Neutered classes: final final public class Motorcycle By putting final in the class definition, it can never have subclasses Similarly, by putting final in front of a method name, its child classes can not overwrite the method