Chapter 7 Generalization/Specialization and Inheritance
description
Transcript of Chapter 7 Generalization/Specialization and Inheritance
![Page 1: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/1.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 1
Chapter 7
Generalization/Specialization and Inheritance
![Page 2: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/2.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 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
![Page 3: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/3.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 3
Implementing the Boat Generalization/Specialization
Hierarchy• Boat Class (See Figure 7-2)
– Includes:• 4 attributes• Constructor invokes 4 accessor (setter) methods• 8 standard accessor methods
– 4 setters– 4 getters
![Page 4: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/4.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 4
![Page 5: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/5.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 5
Implementing the Boat Generalization/Specialization
Hierarchy• Testing the Boat Superclass
– TesterOne tester program (Figure 7-3)• Creates three boat instances• Retrieves information about each instance
– Sequence diagram (Figure 7-5)• Shows how TesterOne interacts with the Boat class
– Boat is ready to use as a superclass
![Page 6: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/6.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 6
![Page 7: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/7.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 7
Implementing the Boat Generalization/Specialization
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
![Page 8: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/8.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 8
Implementing the Boat Generalization/Specialization
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);
![Page 9: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/9.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 9
![Page 10: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/10.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 10
Implementing the Boat Generalization/Specialization
Hierarchy• Using the Extends Keyword to Create
the Sailboat Subclass– To use inheritance:
• Need to know constructor and method signatures (APIs)
• Do not need:– Access to source code– Knowledge of how class is written
![Page 11: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/11.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 11
Implementing the Boat Generalization/Specialization
Hierarchy• Testing the Sailboat Subclass
– TesterTwoA tester program (Figure 7-8)• Creates 3 Sailboat instances and populates
them• Can use any methods defined in super or sub
class– Sequence diagram (Figure 7-10)
• Shows how TesterTwoA interacts with the Sailboat class
![Page 12: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/12.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 12
![Page 13: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/13.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 13
Implementing the Boat Generalization/Specialization
Hierarchy• Adding a Second Subclass – Powerboat
– Powerboat Class (Figure 7-11)• Second class can be extended from Boat without
effecting any other classes derived from Boat– TesterTwoB tester program (Figure 7-12)
• Creates instances of both subclass and populates them– Sequence diagram (Figure 7-14)
• Shows how TesterTwoB interacts with the Sailboat and Powerboat classes
![Page 14: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/14.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 14
![Page 15: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/15.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 15
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» e.g., public abstract class Boat
![Page 16: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/16.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 16
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» e.g. public final class Boat
![Page 17: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/17.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 17
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
• Compare overriding with overloading
![Page 18: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/18.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 18
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– Compare tellAboutSelf() in Figures 7-15, 7-
16
![Page 19: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/19.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 19
Overriding a Superclass Method
• Overriding & Invoking a Superclass Method– Two basic ways to override a superclass
method• Override completely – Saleboat 7-16
– Redefine what the method does• Override and append – Powerboat 7-19
– Override method– Call super to execute superclass method– Add additional functionality to overridden method
![Page 20: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/20.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 20
Overriding a Superclass Method
• Testing Two Method-Overriding Approaches– TesterThreeB tester program (Figure 7-20)
• Creates instances of both subclass, populates them, and demonstrates method overriding
– Sequence diagram• Shows how TesterThreeB interacts with the
Sailboat and Powerboat classes
![Page 21: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/21.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 21
![Page 22: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/22.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 22
Overriding a Superclass Method
• Overriding, Polymorphism, and Dynamic Binding– 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
![Page 23: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/23.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 23
Overriding a Superclass Method
• Overriding, Polymorphism, and Dynamic Binding– Dynamic Binding
• Resolves which method to invoke at runtime if overridden methods exist
• Provides flexibility when adding new subclasses to a system
![Page 24: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/24.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 24
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
![Page 25: Chapter 7 Generalization/Specialization and Inheritance](https://reader033.fdocuments.in/reader033/viewer/2022061414/56815bd4550346895dc9c476/html5/thumbnails/25.jpg)
Chapter 7 - Generalization/Specialization and Inheritance 25
Understanding Private Versus Protected Access
• Protected access– Subclass has direct access to protected
variables of superclass – pp. 236• 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