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

Post on 04-Jan-2016

216 views 0 download

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

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?

LECTURE 8:INHERITANCE

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

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

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” 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?)

extends Example

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

Vehicle

Automobile

Car Truck

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

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

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

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

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?

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

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

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