Download presentation
Presentation is loading. Please wait.
Published byLindsey Gilmore Modified over 9 years ago
1
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?
3
Sharing Among Classes
4
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
5
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); }
6
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); }
7
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 ?)
8
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
9
extends extends Example Each class extends exactly one class extends extends explicitly specifies superclass If not stated, class defaults to subclass of Object VehicleAutomobileCarTruck
10
extends extends Example Each class extends exactly one class extends extends explicitly specifies superclass If not stated, class defaults to subclass of Object ObjectVehicleAutomobileCarTruck
11
extends extends Example Extended by as many classes as heart desires Automobile superclass of Car, Truck Vehicle superclass of Automobile, Car, Truck ObjectVehicleAutomobileCarTruck
12
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
13
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
14
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);
15
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);
16
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);
17
What Gets Inherited And How?
18
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
19
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
20
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
21
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());
22
Your Turn Get into your groups and complete activity
23
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.