Chapter 6 Inheritance Inheritance Hierarchies Modeling Specialization and

  • Slides: 23
Download presentation
Chapter 6 Inheritance

Chapter 6 Inheritance

Inheritance Hierarchies Modeling Specialization and Generalization ü Real world: Hierarchies describe general/specific relationships §

Inheritance Hierarchies Modeling Specialization and Generalization ü Real world: Hierarchies describe general/specific relationships § General concept at root of tree § More specific concepts are children ü Programming: Inheritance hierarchy § General superclass at root of tree § More specific subclasses are children ü When designing systems, look for instances of generalization and specialization

Example Hierarchy of Employee Classes

Example Hierarchy of Employee Classes

The Substitution Principle (1) ü Formulated by Barbara Liskov ü You can use a

The Substitution Principle (1) ü Formulated by Barbara Liskov ü You can use a subclass object whenever a superclass object is expected example: Employee e; e = new Manager(“Barnie Smith”); . . . System. out. println("salary=" + e. get. Salary()); ü Can set e to Manager reference ü Polymorphism: Correct get. Salary method is invoked

The Substitution Principle (2) ü In Java, the type rules always allow a subclass

The Substitution Principle (2) ü In Java, the type rules always allow a subclass object to be used when a superclass object is expected. ü However, the question is whether the subclass object can replace the superclass one, conceptually! ü Don't use inheritance if substitution principle is violated

Invoking Superclass Methods (1) ü Can't access private fields of superclass (If salary is

Invoking Superclass Methods (1) ü Can't access private fields of superclass (If salary is a private fields in Employee) public class Manager extends Employee { public double get. Salary() { return salary + bonus; // ERROR--private field }. . . }

Invoking Superclass Methods (2) ü (1 st Trial to Solve) Be careful when calling

Invoking Superclass Methods (2) ü (1 st Trial to Solve) Be careful when calling superclass method public double get. Salary() { return get. Salary() + bonus; // ERROR--recursive call }

Invoking Superclass Methods (3) ü (Solution) Use super keyword public double get. Salary() {

Invoking Superclass Methods (3) ü (Solution) Use super keyword public double get. Salary() { return super. get. Salary() + bonus; } ü Can you do super? § No, super is not a reference

Invoking Superclass Methods (4) ü (Another Trial to Solve) public class Manager extends Employee

Invoking Superclass Methods (4) ü (Another Trial to Solve) public class Manager extends Employee {. . . private double salary; //ERROR-replicated field }

Invoking Superclass Constructors ü Use super keyword in subclass constructor: public Manager(String a. Name)

Invoking Superclass Constructors ü Use super keyword in subclass constructor: public Manager(String a. Name) { super(a. Name); // calls superclass constructor bonus = 0; } ü Call to super must be first statement in subclass constructor ü If a subclass constructor does not call a superclass constructor, then the superclass constructor with no parameters is called automatically.

Hierarchy of Swing Components (1) ü Base of hierarchy: Component ü Most important subclass:

Hierarchy of Swing Components (1) ü Base of hierarchy: Component ü Most important subclass: Container

Hierarchy of Swing Components (2)

Hierarchy of Swing Components (2)

Graphic Programming with Inheritance ü Chapter 4: Create drawings by implementing Icon interface type

Graphic Programming with Inheritance ü Chapter 4: Create drawings by implementing Icon interface type ü Now: Form subclass of JComponent public class My. Component extends JComponent { public void paint. Component(Graphics g) { drawing instructions go here }. . . } ü Advantage: Inherit behavior from JComponent § Example: Can attach mouse listener to JComponent

Graphic Programming with Inheritance Overriding paint. Component (1) ü Draw a car: public class

Graphic Programming with Inheritance Overriding paint. Component (1) ü Draw a car: public class Car. Component extends JComponent { public void paint. Component(Graphics g) { Graphics 2 D g 2 = (Graphics 2 D)g; car. draw(g 2); }. . . private Car. Shape car; }

Mouse Listeners To Complete the car drawing program (1) ü Attach mouse listener to

Mouse Listeners To Complete the car drawing program (1) ü Attach mouse listener to component ü Can listen to mouse events (clicks) or mouse motion events public interface Mouse. Listener { void mouse. Clicked(Mouse. Event event); void mouse. Pressed(Mouse. Event event); void mouse. Released(Mouse. Event event); void mouse. Entered(Mouse. Event event); void mouse. Exited(Mouse. Event event); }

Mouse Listeners To Complete the car drawing program (2) public interface Mouse. Motion. Listener

Mouse Listeners To Complete the car drawing program (2) public interface Mouse. Motion. Listener { void mouse. Moved(Mouse. Event event); void mouse. Dragged(Mouse. Event event); }

Mouse Adapter To Complete the car drawing program (3) ü What if you just

Mouse Adapter To Complete the car drawing program (3) ü What if you just want to listen to mouse. Pressed? Listener interface types with many methods have corresponding adapter classes with donothing methods. Extend the adapter rather than implementing the listener.

Mouse Adapter To Complete the car drawing program (4) public class Mouse. Adapter implements

Mouse Adapter To Complete the car drawing program (4) public class Mouse. Adapter implements Mouse. Listener { public void mouse. Clicked(Mouse. Event event) {} public void mouse. Pressed(Mouse. Event event) {} public void mouse. Released(Mouse. Event event) {} public void mouse. Entered(Mouse. Event event) {} public void mouse. Exited(Mouse. Event event) {} }

Mouse Adapter To Complete the car drawing program (5) ü Extend Mouse. Adapter ü

Mouse Adapter To Complete the car drawing program (5) ü Extend Mouse. Adapter ü Component constructor adds listener: add. Mouse. Listener(new Mouse. Adapter() { public void mouse. Pressed(Mouse. Event event) { mouse action goes here } });

Car Drawing Program ü ch 6/car/Car. Shape. java ü ch 6/car/Car. Component. java ü

Car Drawing Program ü ch 6/car/Car. Shape. java ü ch 6/car/Car. Component. java ü ch 6/car/Car. Mover. java

Abstract Classes (1) ü An abstract method is undefined and must be defined in

Abstract Classes (1) ü An abstract method is undefined and must be defined in a subclass. ü A class with one or more abstract class methods must be declared as an abstract class. public abstract class Selectable. Shape implements Scene. Shape {…}

Abstract Classes (2) ü You can not construct an object of an abstract class.

Abstract Classes (2) ü You can not construct an object of an abstract class. § Selectable. Shape shape = new Selectable. Shape(); //Error § Selectable. Shape shape = new House. Shape(); //OK

Abstract Classes (3) ü Scene. Shape. java ü Selectable. Shape. java ü House. Shape.

Abstract Classes (3) ü Scene. Shape. java ü Selectable. Shape. java ü House. Shape. java ü Car. Shape. java ü Scene. Panel. java ü Scene. Editor. java