Download presentation
Presentation is loading. Please wait.
1
Chapter 6 Inheritance
2
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
3
Example Hierarchy of Employee Classes
4
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.getSalary()); Can set e to Manager reference Polymorphism: Correct getSalary method is invoked
5
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
6
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 getSalary() { return salary + bonus; // ERROR--private field } ... }
7
Invoking Superclass Methods (2)
(1st Trial to Solve) Be careful when calling superclass method public double getSalary() { return getSalary() + bonus; // ERROR--recursive call }
8
Invoking Superclass Methods (3)
(Solution) Use super keyword public double getSalary() { return super.getSalary() + bonus; } Can you do super.super? No, super is not a reference
9
Invoking Superclass Methods (4)
(Another Trial to Solve) public class Manager extends Employee { ... private double salary; //ERROR-replicated field }
10
Invoking Superclass Constructors
Use super keyword in subclass constructor: public Manager(String aName) { super(aName); // 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.
11
Hierarchy of Swing Components (1)
Base of hierarchy: Component Most important subclass: Container
12
Hierarchy of Swing Components (2)
13
Graphic Programming with Inheritance
Chapter 4: Create drawings by implementing Icon interface type Now: Form subclass of JComponent public class MyComponent extends JComponent { public void paintComponent(Graphics g) { drawing instructions go here } ... } Advantage: Inherit behavior from JComponent Example: Can attach mouse listener to JComponent
14
Graphic Programming with Inheritance Overriding paintComponent (1)
Draw a car: public class CarComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; car.draw(g2); } private CarShape car; }
15
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 MouseListener { void mouseClicked(MouseEvent event); void mousePressed(MouseEvent event); void mouseReleased(MouseEvent event); void mouseEntered(MouseEvent event); void mouseExited(MouseEvent event); }
16
Mouse Listeners To Complete the car drawing program (2)
public interface MouseMotionListener { void mouseMoved(MouseEvent event); void mouseDragged(MouseEvent event); }
17
Mouse Adapter To Complete the car drawing program (3)
What if you just want to listen to mousePressed? Listener interface types with many methods have corresponding adapter classes with do-nothing methods. Extend the adapter rather than implementing the listener.
18
Mouse Adapter To Complete the car drawing program (4)
public class MouseAdapter implements MouseListener { public void mouseClicked(MouseEvent event) {} public void mousePressed(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} }
19
Mouse Adapter To Complete the car drawing program (5)
Extend MouseAdapter Component constructor adds listener: addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { mouse action goes here } });
20
Car Drawing Program ch6/car/CarShape.java ch6/car/CarComponent.java
ch6/car/CarMover.java
21
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 SelectableShape implements SceneShape { … }
22
Abstract Classes (2) You can not construct an object of an abstract class. SelectableShape shape = new SelectableShape(); //Error SelectableShape shape = new HouseShape(); //OK
23
Abstract Classes (3) SceneShape.java SelectableShape.java
HouseShape.java CarShape.java ScenePanel.java SceneEditor.java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.