Question of the Day There are two escalators at each subway stop. One going up & one down. Whenever an escalator needs to be fixed, they almost always make the working escalator go up. Why?
Sharing Among Classes
Composition has-a Used when there is “has-a” relationship has a Student has a name has a Full name has a first name has an Car has an engine has an Rectangle has an upper right vertex Use a field to compose classes So we would add name field to Student class firstName field in FullName class If must use data externally, add getters & setters
Composition Example public class FullName { private String firstName; private String lastName; // constructor, getters & setters also here } public class Student { private FullName name; // Brevity is the soul of wit- Shakespeare public String getFirstName() { return name.getFirstName(); } public void setFirstName(String newName) { name.setFirstName(newName); }
Composition Example public class FullName { private String firstName; private String lastName; // constructor, getters & setters also here } public class Student { private FullName name; // Brevity is the soul of wit- Shakespeare public String getFirstName() { return name.getFirstName(); } public void setFirstName(String newName) { name.setFirstName(newName); }
Inheritance Is-a “Is-a” relationships implemented via inheritance is a Automobile is a Vehicle is a Car is a Vehicle is an Truck is an Automobile is an Car is an Automobile extends Starts with superclass which subclass extends extends Automobile extends Vehicle extends Truck extends Automobile extends Car extends Automobile (& Vehicle ?)
extends extends Example extends extends extends public class Vehicle {…} public class Automobile extends Vehicle {…} public class Car extends Automobile {…} public class Truck extends Automobile {…} VehicleAutomobileCarTruck
extends extends Example Each class extends exactly one class extends extends explicitly specifies superclass If not stated, class defaults to subclass of Object VehicleAutomobileCarTruck
extends extends Example Each class extends exactly one class extends extends explicitly specifies superclass If not stated, class defaults to subclass of Object ObjectVehicleAutomobileCarTruck
extends extends Example Extended by as many classes as heart desires Automobile superclass of Car, Truck Vehicle superclass of Automobile, Car, Truck ObjectVehicleAutomobileCarTruck
Superclass (& Subclass) Every class is subclass of: superclass superclass’s superclass superclass’s superclass’s superclass superclass’s superclass’s superclass’s superclass, … Object is-ais-a Truck is-a Automobile is-a Vehicle is-a So Truck is-a Object also
What Gets Inherited And How? Class inherits all members from superclass Subclass can use them directly unless they are private Use as if they were copied into class w/o copying
Inheritance Example public class Sailor { private String str = "Aye, aye"; public String getMyString() { return "Capt."; } } public class Pirate extends Sailor { public String getFullString() { return “not ” + getMyString(); } public Pirate() { str = "Yarr"; } public static void main(String[] args) { Pirate sub = new Pirate(); Sailor ship = new Sailor(); System.out.println(sub.getMyString()); System.out.println(ship.getMyString()); System.out.println(sub.getFullString()); System.out.println(sub.str); System.out.println(ship.str);
Inheritance Example public class Sailor { private String str = "Aye, aye"; public String getMyString() { return "Capt."; } } public class Pirate extends Sailor { public String getFullString() { return “not ” + getMyString(); } public Pirate() { str = "Yarr"; } public static void main(String[] args) { Pirate sub = new Pirate(); Sailor ship = new Sailor(); System.out.println(sub.getMyString()); System.out.println(ship.getMyString()); System.out.println(sub.getFullString()); System.out.println(sub.str); System.out.println(ship.str);
Inheritance Example public class Sailor { protected String str = "Aye, aye"; public String getMyString() { return "Capt."; } } public class Pirate extends Sailor { public String getFullString() { return “not ” + getMyString(); } public Pirate() { str = "Yarr"; } public static void main(String[] args) { Pirate sub = new Pirate(); Sailor ship = new Sailor(); System.out.println(sub.getMyString()); System.out.println(ship.getMyString()); System.out.println(sub.getFullString()); System.out.println(sub.str); System.out.println(ship.str);
What Gets Inherited And How?
Overriding Methods Subclass can redeclare inherited method Overloaded when different signature used Method is called overridden if signatures identical Specialize method execution in subclass w/this Changed only for subclass & its subclasses Original used by superclass & other classes Actual method called determined by instance’s type
Overriding Methods Call overriden method as defined in superclass Only in subclass overriding the method If method is not overriden then this is not needed super super.methodName(…) used to call method supersuper No multiples: super.super.methodName(…) Cannot make less accessible when overriding However, method can make more accessible
Overriding Methods Call overriden method as defined in superclass Only in subclass overriding the method If method is not overriden then this is not needed super super.methodName(…) used to call method supersuper No multiples: super.super.methodName(…) Cannot make less accessible when overriding However, method can make more accessible
Inheritance Example public class SuperClass { public String getMyString() { return “SUPER”; } } public class SubClass extends SuperClass { public String getMyString() { return “sub ”; } public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass super1 = new SuperClass(); System.out.println(sub.getMyString()); System.out.println(super1.getMyString()); System.out.println(sub.getMyString());
Your Turn Get into your groups and complete activity
For Next Lecture 1 st quiz in class on Monday Covers everything from first 3 weeks of school Focus will be on objects, fields, methods, types,… Problems like on activities, weekly assignments, more There is weekly assignment problem on Angel Due by 5PM Tuesday (via Assignment Submitter) Both problems graded using provided JUnit tests