Chapter 7 GeneralizationSpecialization and Inheritance Dr' James Jiang - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Chapter 7 GeneralizationSpecialization and Inheritance Dr' James Jiang

Description:

Using the Extends Keyword to Create the Sailboat Subclass ... Creates 3 Sailboat instances and populates them ... Sailboat class that overrides the ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 35
Provided by: riche2
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 GeneralizationSpecialization and Inheritance Dr' James Jiang


1
Chapter 7Generalization/Specialization and
InheritanceDr. James Jiang
2
Chapter 7 Topics
  • Creating generalization/specialization
    hierarchies using the extends keyword to
    implement inheritance with problem domain classes
  • Invoking superclass constructors when writing a
    subclass constructor
  • Using the abstract and final keywords with Java
    superclasses
  • Overriding superclass methods in subclasses
  • How polymorphism works with Java subclasses
  • The implications of protected and private access
    in superclasses

3
Implementing the Boat Generalization/Specializatio
n Hierarchy
  • Boat Class
  • Includes
  • 4 attributes
  • Constructor ? invokes 4 accessor (setter) methods
  • 8 standard accessor methods
  • 4 setters
  • 4 getters

4
(No Transcript)
5
Initial Boat class (I)
6
Initial Boat class (II)
7
Implementing the Boat Generalization/Specializatio
n Hierarchy
  • Testing the Boat Superclass
  • TesterOne tester program
  • Creates three boat instances
  • Retrieves information about each instance
  • Sequence diagram
  • Shows how TesterOne interacts with the Boat class
  • Boat is ready to use as a superclass

8
TesterOne.java
9
Implementing the Boat Generalization/Specializatio
n Hierarchy
  • Using the Extends Keyword to Create the Sailboat
    Subclass
  • Generalization/specialization hierarchy
  • General superclass includes attributes and
    methods that are shared by specialized subclasses
  • Instances of subclass inherit the attributes and
    methods of the superclass
  • Include additional attributes and methods defined
    by subclass

10
Implementing the Boat Generalization/Specializatio
n Hierarchy
  • Using the Extends Keyword to Create the Sailboat
    Subclass
  • Keywords
  • Use extends keyword to implement inheritance
  • public class Sailboat extends Boat
  • Use super keyword to invoke superclass
    constructor
  • super(aStateRegNo, aLength, aManufacturer, aYear)

11
(No Transcript)
12
Implementing the Boat Generalization/Specializatio
n Hierarchy
  • Using the Extends Keyword to Create the Sailboat
    Subclass
  • To use inheritance
  • Need to know constructor and method signatures
  • Do not need
  • Access to source code
  • Knowledge of how class is written

13
Sailboat class definition (I)
14
Sailboat class definition (II)
15
Implementing the Boat Generalization/Specializatio
n Hierarchy
  • Testing the Sailboat Subclass
  • TesterTwoA tester program
  • Creates 3 Sailboat instances and populates them
  • Can use any methods defined in super or sub class
  • Sequence diagram
  • Shows how TesterTwoA interacts with the Sailboat
    class

16
TesterTwoA.java
17
Implementing the Boat Generalization/Specializatio
n Hierarchy
  • Adding a Second Subclass Powerboat
  • Powerboat Class
  • Second class can be extended from Boat without
    effecting any other classes derived from Boat
  • TesterTwoB tester program
  • Creates instances of both subclass and populates
    them
  • Sequence diagram
  • Shows how TesterTwoB interacts with the Sailboat
    and Powerboat classes

18
Powerboat class definition
19
Understanding Abstract and Final Classes
  • Using Abstract Classes
  • Concrete class
  • Class that can be instantiated
  • Abstract class
  • Class not intended to be instantiated
  • Used only to extend into subclasses
  • Facilitates reuse
  • Keyword
  • abstract
  • Used in class header to declare an abstract class

20
Understanding Abstract and Final Classes
  • Using Final Classes
  • Final class
  • Class not intended to be extended
  • Improves performance ? JVM does not have to
    search for subclass methods that might override
    superclass methods
  • Implements security by restricting class
    extension
  • Keyword
  • final
  • Used in class header to declare a final class

21
Overriding a Superclass Method
  • Method Overriding
  • Occurs when method in subclass is invoked instead
    of method in superclass
  • Both methods have the same signature
  • Allows subclass to modify the behavior of the
    superclass

22
Overriding a Superclass Method
  • Adding Overriding tellAboutSelf Method
  • Use same tellAboutSelf method signature in
    subclass
  • Implement new functionality in overridden method
    definition
  • Can invoke methods from super or sub class

23
Abstract Boat class with tellAboutSelf method
24
Overriding a Superclass Method
  • Overriding Invoking a Superclass Method
  • Two basic ways to override a superclass method
  • Override completely
  • Redefine what the method does
  • Override and append
  • Override method
  • Call super to execute superclass method
  • Add additional functionality to overridden method

25
Sailboat class that overrides the tellAboutSelf
method (I)
26
Sailboat class that overrides the tellAboutSelf
method (II)
27
Overriding a Superclass Method
  • Testing Two Method-Overriding Approaches
  • TesterThreeB tester program
  • Creates instances of both subclass, populates
    them, and demonstrates method overriding
  • Sequence diagram
  • Shows how TesterThreeB interacts with the
    Sailboat and Powerboat classes

28
TesterThreeA.java
29
Overriding a Superclass Method
  • Overriding and Polymorphism
  • Polymorphism
  • Objects of different classes can respond to the
    same message in their own way
  • Simplifies processing
  • Responsibility of responding appropriately is
    left to the particular instance
  • Method overriding
  • Form of polymorphism

30
Powerboat class overrides and invokes
tellAboutSelf method (I)
31
Powerboat class overrides and invokes
tellAboutSelf method (II)
32
TesterThreeB.java
33
Understanding Private Versus Protected Access
  • Private access
  • No other object can directly read or modify the
    value of an attribute
  • Must use accessor methods
  • Also limits ability of subclass to directly
    access private variables of superclass
  • Must use accessor methods

34
Understanding Private Versus Protected Access
  • Protected access
  • Subclass has direct access to private variables
    of superclass
  • Other classes in same package have direct access
    as well
  • Can also use with methods
  • Use with care
  • Avoid use of public keyword with attributes
  • Violates encapsulation and information hiding
Write a Comment
User Comments (0)
About PowerShow.com