Question of the Day There are two escalators at each subway stop. One going up & one down. ...

23
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?

Transcript of Question of the Day There are two escalators at each subway stop. One going up & one down. ...

Page 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.

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?

Page 2: 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.

LECTURE 8:INHERITANCE

Page 3: 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.

Sharing Among Classes

Classes often share actions & data Writing code once is laziest option

Cutting-and-pasting still requires effort Increase bugs with multiple copies of code

Two ways to not rewrite code:

Inheritance&

Composition

Page 4: 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.

Composition

Used when there is “has-a” relationship Student has a name Full name has a first name Car has an engine 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

Page 5: 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.

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);}

Page 6: 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.

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);}

Page 7: 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.

Inheritance

“Is-a” relationships implemented via inheritance Automobile is a Vehicle Car is a Vehicle Truck is an Automobile Car is an Automobile

Starts with superclass which subclass extends Automobile extends Vehicle Truck extends Automobile Car extends Automobile (& Vehicle?)

Page 8: 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.

extends Example

public class Vehicle {…}public class Automobile extends Vehicle {…}public class Car extends Automobile {…}public class Truck extends Automobile {…}

Vehicle

Automobile

Car Truck

Page 9: 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.

extends Example

Each class extends exactly one class extends explicitly specifies superclass If not stated, class defaults to subclass of Object

Vehicle

Automobile

Car Truck

Page 10: 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.

extends Example

Each class extends exactly one class extends explicitly specifies superclass If not stated, class defaults to subclass of Object Object

Vehicle

Automobile

Car Truck

Page 11: 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.

extends Example

Extended by as many classes as heart desires Automobile superclass of Car, Truck Vehicle superclass of Automobile, Car, Truck

Object

Vehicle

Automobile

Car Truck

Page 12: 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.

Superclass (& Subclass)

Every class is subclass of: superclass superclass’s superclass superclass’s superclass’s superclass superclass’s superclass’s superclass’s

superclass, … Object

Truck is-a Automobile is-a Vehicle So Truck is-a Object also

Page 13: 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.

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

Page 14: 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.

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);

Page 15: 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.

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);

Page 16: 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.

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);

Page 17: 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.

What Gets Inherited And How?

Inheritance is part of extends and automatic DON’T RETYPE IN SUBCLASS

Redeclaring fields leads to bad things Yields way of specializing when redeclaring

methods

Page 18: 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.

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

Page 19: 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.

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.methodName(…) used to call method No multiples: super.super.methodName(…)

Cannot make less accessible when overriding However, method can make more accessible

Page 20: 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.

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.methodName(…) used to call method No multiples: super.super.methodName(…)

Cannot make less accessible when overriding However, method can make more accessible

Page 21: 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.

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());

Page 22: 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.

Your Turn

Get into your groups and complete activity

Page 23: 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.

For Next Lecture

1st 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