Download presentation
Presentation is loading. Please wait.
1
Class Inheritance Part I
Corresponds with Liang Chapter 10
2
What is Inheritance? Superclasses and subclasses
The subclass inherits the members of the base class All instance member variables for the superclass will be present in an instance of the subclass All instance methods of the superclass can be called with respect to an instance of the subclass
3
Implementing Inheritance in Java
The extends keyword specifies the superclass for a class If class1 extends class2, then class1 will be a subclass of class2, so class1 will inherit all the members of class2. The this keyword Refers to the current class The super keyword Refers to the superclass Can use it to call the superclass constructor Can use it to access a superclass method when the subclass overrides this method Accessing superclass members The statements in the subclass methods can access any non-private (i.e. public or protected) members of the superclass.
4
Superclasses and Subclasses
There are similar examples in chapter 10
5
A Cylinder1 class as a subclass of Circle
Extends specifies that Cylindar1’s superclass is Circle The superclass The subclass
6
A Cylinder1 class as a subclass of Circle
Here, the super keyword is used to invoke the superclass’s constructor. The subclass The superclass
7
A Cylinder1 class as a subclass of Circle
Here, the subclass method is calling the inherited method of the superclass. The superclass The subclass
8
A Cylinder1 class as a subclass of Circle
If a local variable or parameter has the same name as a member variable, use the this keyword to resolve the ambiguity. The superclass The subclass
9
A Cylinder1 class as a subclass of Circle
Invoke constructor An application using the Cylinder1 class
10
A Cylinder1 class as a subclass of Circle
Calling subclass methods An application using the Cylinder class
11
A Cylinder1 class as a subclass of Circle
Calling inherited superclass methods An application using the Cylinder class
12
A Cylinder1 instance is created on the heap…Note that the instance has the Cylinder1 class’s length member variable AND it inherits the Circle class’s radius member variable. main’s frame Cylinder1 object args radius myCylinder length Frame Stack Heap
13
Frame Stack Heap Cylinder1 frame this radius 5.0 length 2.0
constructor frame this radius 5.0 length 2.0 Cylinder1’s constructor is invoked with respect to the newly created instance. main’s frame Cylinder1 object args radius myCylinder length Frame Stack Heap
14
Frame Stack Heap 5.0 this Cylinder1 frame this radius 5.0 length 2.0
Circle constructor frame newRadius 5.0 this Cylinder1 constructor frame this radius 5.0 length 2.0 Circle’s constructor is invoked with respect to the newly created instance, and executes. main’s frame Cylinder1 object args radius 5.0 myCylinder length Frame Stack Heap
15
Cylinder1’s constructor executes after Circle’s constructor terminates.
frame this radius 5.0 length 2.0 main’s frame Cylinder1 object args radius 5.0 myCylinder length 2.0 Frame Stack Heap
16
After constructors terminate, the results from new are assigned into myCylinder.
main’s frame Cylinder1 object args radius 5.0 myCylinder length 2.0 Frame Stack Heap
17
Frame Stack Heap Later in the program…… Circle frame this Cylinder1
findVolume frame this Circle findArea frame this main’s frame Cylinder object args radius 5.0 myCylinder length 2.0 Frame Stack Heap
18
Advantages of Inheritance
No need to “reinvent the wheel” Can make use of pre-existing data and functionality In previous example: radius member variable was inherited code for initializing radius (in constructor) was inherited code for calculating area was inherited Can “specialize” the inherited data and functionality length member variable was created for subclass code for initializing length (in subclass constructor) was created code for calculating volume was created (making use of inherited area calculation)
19
Overriding Methods A subclass can override the methods of its superclass Overriding is not the same as overloading! Overriding = same signature as superclass method, with different method body and declared at the subclass. If you want specialized behavior at the subclass, override the superclass method. If you don’t want specialized behavior at the subclass, simply use the inherited superclass method.
20
A Cylinder class that overrides the Circleclass’s findArea method
Here, the subclass has its own findArea method…this overrides the findArea method of the superclass.
21
A Cylinder class that overrides the Circleclass’s findArea method
Can still make use of the superclass’s version of the overridden method through use of the super keyword..
22
Unified Modeling Language
Static class diagram: Boxes for representing classes Top part of box contains name Middle part contains attributes Lower part contains operations Solid line with empty arrow describes inheritance.
23
UML Diagram for Cylinder
Inheritance relationship is shown via a line with an triangular arrow from the subclass to the superclass. Classes Attributes Operations Visio drawing of UML diagram
24
If the findArea method is called on an instance of Cylinder……
Cylinder’s findArea frame this Circle’s findArea frame this main’s frame Cylinder object args radius 5.0 myCylinder length 2.0 Frame Stack Heap
25
In-Class Group Exercise
Create a subclass of Circle called Sphere. This class should inherit everything from Circle, but should override the findArea method and should include its own findVolume method. The only difference between a Sphere and a Circle is the calculations of its dimensions: The area of a sphere is: 4 * PI * r2 The volume of a sphere is: 4/3 * PI * r3 Draw a UML diagram, and the write the code for the class.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.