0% found this document useful (0 votes)
128 views

OOProg21 - Introduction To Inheritance

Inheritance allows one class to inherit attributes and behavior from another class. The keyword "extends" is used in Java to achieve inheritance. Polymorphism allows the same method name to have different implementations. A subclass can access superclass methods using the keyword "super" and can employ information hiding by keeping data private. Certain methods like static, final, and those in a final superclass cannot be overridden.

Uploaded by

giselejoyp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

OOProg21 - Introduction To Inheritance

Inheritance allows one class to inherit attributes and behavior from another class. The keyword "extends" is used in Java to achieve inheritance. Polymorphism allows the same method name to have different implementations. A subclass can access superclass methods using the keyword "super" and can employ information hiding by keeping data private. Certain methods like static, final, and those in a final superclass cannot be overridden.

Uploaded by

giselejoyp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to

Inheritance
Objectives
• Learn about the concept of inheritance
• Extend classes
• Override superclass methods
• Call constructors during inheritance
• Access superclass methods
• Employ information hiding
• Learn which methods you cannot override

2
Learning About the Concept
of Inheritance
• Inheritance
– A mechanism that enables one class to inherit both the
behavior and the attributes of another class
– Apply your knowledge of a general category to more
specific objects

3
Diagramming Inheritance Using
the UML
• Unified Modeling Language (UML)
– Consists of many types of diagrams
• Class diagram
– A visual tool
– Provides an overview of a class

4
Diagramming Inheritance Using
the UML (cont’d.)

5
Diagramming Inheritance Using
the UML (cont’d.)

6
Diagramming Inheritance Using
the UML (cont’d.)
• Use inheritance to create a derived class
– Saves time
– Reduces errors
– Reduces the amount of new learning required to use a
new class

7
Inheritance Terminology
• Base class
– Used as a basis for inheritance
– Also called:
• Superclass
• Parent class

8
Inheritance Terminology (cont’d.)
• Derived class
– Inherits from a base class
– Always “is a” case or an example of a more general base
class
– Also called:
• Subclass
• Child class

9
Extending Classes
• Keyword extends
– Used to achieve inheritance in Java
– Example:
public class EmployeeWithTerritory extends
Employee
• Inheritance is a one-way proposition
– A child inherits from a parent, not the other way around
• Subclasses are more specific
• instanceof operator

10
Extending Classes (cont’d.)

11
Overriding Superclass Methods
• Create a subclass by extending an existing class
– A subclass contains data and methods defined in the
original superclass
– Sometimes superclass data fields and methods are not
entirely appropriate for subclass objects
• Polymorphism
– The same method name is used to indicate different
implementations

12
Overriding Superclass Methods
(cont’d.)
• Override the method in the parent class
– Create a method in a child class that has the same name
and parameter list as a method in its parent class
• Subtype polymorphism
– The ability of one method name to work appropriately for
different subclass objects of the same parent class
• Override annotation (@Override)
– Tells compiler you are overriding a parent class method

13
Calling Constructors During
Inheritance
• When you instantiate an object that is a member of a
subclass, you call at least two constructors:
– The constructor for the base class
– The constructor for the extended class
• The superclass constructor must execute first
• When the superclass contains a default constructor,
the execution of the superclass constructor is
transparent

14
Calling Constructors During
Inheritance (cont’d.)

15
Calling Constructors During
Inheritance (cont’d.)

16
Using Superclass Constructors
That Require Arguments
• When you write your own constructor
– You replace the automatically supplied version
• When extending a superclass with constructors that
require arguments
– The subclass must provide the superclass constructor with
the arguments it needs

17
Using Superclass Constructors
That Require Arguments (cont’d.)
• When a superclass has a default constructor
– You can create a subclass with or without its own
constructor
• When a superclass contains only constructors that
require arguments
– You must include at least one constructor for each subclass
you create
• The first statement within each constructor must call one of the
superclass constructors

18
Using Superclass Constructors
That Require Arguments (cont’d.)
• Call the superclass constructor
– super(list of arguments);
• Keyword super
– Always refers to the superclass

19
Accessing Superclass Methods
• Use the overridden superclass method within a
subclass
– Use the keyword super to access the parent class
method

20
Accessing Superclass Methods
(cont’d.)

21
Comparing this and super
• Think of the keyword this as the opposite of
super within a subclass
• When a parent class contains a method that is not
overridden
– The child can use the method name with super or this,
or alone

22
Employing Information Hiding
• Within the Student class:
– The keyword private precedes each data field
– The keyword public precedes each method
• Information hiding
– The concept of keeping data private
– Data can be altered only by methods you choose and only
in ways that you can control

23
Employing Information Hiding
(cont’d.)

24
Employing Information Hiding
(cont’d.)
• When a class serves as a superclass
– Subclasses inherit all data and methods of the superclass
• Except private members of the parent class are not accessible
within a child class’s methods

25
Employing Information Hiding
(cont’d.)
• Keyword protected
– Provides an intermediate level of security between
public and private access
– Can be used within its own class or in any classes extended
from that class
– Cannot be used by “outside” classes

26
Methods You Cannot Override
• static methods
• final methods
• Methods within final classes

27
A Subclass Cannot Override static
Methods in Its Superclass
• A subclass cannot override methods declared
static in the superclass
• A subclass can hide a static method in the
superclass by declaring a static method with the
same signature as the static method in the
superclass
– Then call the new static method from within the
subclass or in another class by using a subclass object
– Within the static method of a subclass, you cannot
access the parent method using the super object

28
A Subclass Cannot Override static
Methods in Its Superclass (cont’d.)
• Although a child class cannot inherit its parent’s
static methods, it can access its parent’s static
methods in the same way any other class can

29
A Subclass Cannot Override static
Methods in Its Superclass (cont’d.)

30
A Subclass Cannot Override final
Methods in Its Superclass
• A subclass cannot override methods declared final
in the superclass
• final modifier
– Does not allow the method to be overridden
• Virtual method calls
– Default in Java
– The method used is determined when the program runs
– The object type might not be known until the method
executes

31
A Subclass Cannot Override final
Methods in Its Superclass (cont’d.)
• Advantages to making the method final:
– The compiler knows only one version of the method exists
– The compiler knows which method version will be used
– A program’s performance can be optimized by removing
calls to final methods
• Inlining the code: each method call is replaced with the expanded
code of the method’s definition

32
A Subclass Cannot Override Methods
in a final Superclass
• When a class is declared final:
– All of its methods are final regardless of which access
modifier precedes the method name
– It cannot be a parent class

33
A Subclass Cannot Override Methods
in a final Superclass (cont’d.)

34
Summary
• Inheritance
– A mechanism that enables one class to inherit both the
behavior and the attributes of another class
• Keyword extends
– Used to achieve inheritance in Java
• Polymorphism
– The act of using the same method name to indicate
different implementations

35
Summary (cont’d.)
• Use a superclass method within a subclass
– Use the keyword super to access it
• Information hiding
– The concept of keeping data private
• Keyword protected
– Provides an intermediate level of security between
public and private access
• A subclass cannot override methods that are:
– Declared static in a superclass
– Declared final or declared within a final class
36

You might also like