lecture 7: inheritance csc 212 – data structures

20
LECTURE 7: INHERITANCE CSC 212 – Data Structures

Upload: patrick-bailey

Post on 18-Dec-2015

228 views

Category:

Documents


2 download

TRANSCRIPT

LECTURE 7:INHERITANCE

CSC 212 – Data Structures

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

Composition &Inheritance

Composition

Used when there is “has-a” relationship Student has a name FullName has a firstName Car has an engine Rectangle has a upperRightVertex

Use a field to compose classes So name would be field in Student class firstName would be field in FullName

class… Use getters & setters to access field’s data

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 an Automobile Truck is an Automobile Car is a Vehicle

Starts with superclass which subclass extends Automobile extends Vehicle Car extends Automobile Truck extends Automobile Car extends Vehicle

extends Keyword

Java example of inheritencepublic class Vehicle {…}public class Wagon extends Vehicle {…}public class Automobile extends Vehicle {…}public class Truck extends Automobile {…}

extends Keyword

Each class extends exactly one class extends explicitly specifies the superclass Otherwise, defaults to subclass of Object

Can be extended by multiple classes Vehicle superclass of Automobile & Wagon Automobile superclass of Truck So, Vehicle also superclass of Truck

Subclass-Superclass

Every class is subclass of: superclass, superclass’s superclass superclass’s superclass’s superclass superclass’s superclass’s superclass superclass, … Object

Truck is-a Automobile which is-a Vehicle So Truck is-a Vehicle also Superclass variables CAN refer to subclass

instances But subclass CANNOT refer to superclass instances

What Gets Inherited And How?

Inherit all fields & methods from superclass Subclass can use them unless they are private Inheritance is automatic – DON’T RETYPE IN

SUBCLASS Redeclaring field or method leads to bad

things

Inheritance Example

public class SuperClass {protected String str = “PARENT”;public String getMyString() { return “SUPER”; }

}public class SubClass extends SuperClass {

public String getFullString() { String s = getMyString(); return “sub” + s;}

public SubClass() { str = “kid”; }

public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass super = new SuperClass(); System.out.println(sub.getMyString()); System.out.println(super.getMyString()); System.out.println(sub.getFullString()); System.out.println(sub.str); System.out.println(super.str); super = sub;

Chaining Constructors

Often want to reuse superclass’s constructor Already initializes fields in superclass Unfortunately, constructors not inherited Maximize laziness: refuse to lose (the

constructor) May also want multiple constructors in

class Often do similar work initializing fields Still want to avoid copying code

Increase laziness through chaining

Chains Explained

Constructors chained by this() or super() Must be first command in constructor Call into same class using this() super() calls superclass constructor

Executes just like a normal method call Must match types listed for the parameters Call cannot violate access protection (e.g., private)

Constructors are not inherited Still need to write constructors Copying code limited by doing this, however

Stickers

public class Sticker {protected String text;public Sticker(String words) { text = words; }public void printMe() { System.out.println(text); }

}

public class CSticker extends Sticker {private String color;public CSticker(String clr, String type) { text = type; color = clr;}

public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe();}

}

Stickers

public class Sticker {protected String text;public Sticker(String words) { text = words; }public void printMe() { System.out.println(text); }

}

public class CSticker extends Sticker {private String color;public CSticker(String clr, String type) { text = type; color = clr;}public CSticker(String typ) { this(“black”, typ);}public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe();}

}

Stickers

public class Sticker {private String text;public Sticker(String words) { text = words; }public void printMe() { System.out.println(text); }

}

public class CSticker extends Sticker {private String color;public CSticker(String clr, String type) { text = type; color = clr;}public CSticker(String typ) { this(“black”, typ);} public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe();}

}

Stickers

public class Sticker {private String text;public Sticker(String words) { text = words; }public void printMe() { System.out.println(text); }

}

public class CSticker extends Sticker {private String color;public CSticker(String clr, String type) { super(type); color = clr;}public CSticker(String typ) { this(“black”, typ);} public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe();}

}

Overriding Methods

Subclass can reuse method names from superclass Overloaded when different signature used When signatures identical its overridden

Subclass can modify exiting methods in this way Changed only for subclass and any of its

subclasses Original used by superclass & other classes Method definition used determined by

instance’s type

Overriding Methods

Can call overriden method as defined in superclass Only in subclass overriding the method Call using super.methodName super.super.methodName illegal (only 1 super

legal) Overriden method cannot become less

accessible Can make more accessible, however

Overriding Example

public class SuperClass {public String getMyString() {return “SUPER”;}public SuperClass() { }

}

public class SubClass extends SuperClass {public String getMyString() {return “sub”;}public SubClass() { }public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass super = sub; System.out.println(sub.getMyString()); System.out.println(super.getMyString()); super = new SuperClass(); System.out.println(super.getMyString());

For Next Lecture

Project #1 has been released Week #3 available on Web/Angel

Do not let it wait, stop procrastinating Class moving into new material

Java review is now over; get help if you need more

I have a cool office with a full candy jar Come by & ask questions while getting sugar

fix