Advanced Java - Lec #5 - Inheritance and Polymorphism.pptx
1. 1
In the name of Allah
Ghalib University
Computer Science Faculty
General Department
Advanced Java
Lecture Title
Inheritance and Polymorphism
Subject: Advanced Java
Instructor: Saddiqi
Date: Saturday, July 5, 2025
Lecture #5
2. List of Contents
• Introduction
• Super-classes and Sub-classes
• Using the super Keyword
• The Object Class and Its toString() Method
• Polymorphism
• The Object’s equals Method
• Summary
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 2
3. Introduction
• Object-oriented programming allows you to define new classes from
existing classes. This is called inheritance.
• Inheritance is an important and powerful feature for reusing
software.
• Suppose you need to define classes to model circles, rectangles, and
triangles. These classes have many common features.
• What is the best way to design these classes so as to avoid redundancy
and make the system easy to comprehend and easy to maintain?
• The answer is to use inheritance.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 3
4. Super-classes and Sub-classes
• Inheritance enables you to define a general class (i.e., a superclass)
and later extend it to more specialized classes (i.e., subclasses).
• In Java terminology, a class C1 extended from another class C2 is
called a subclass, and C2 is called a superclass.
• A superclass is also referred to as a parent class or a base class.
• A subclass as a child class, an extended class, or a derived class.
• A subclass inherits accessible data fields and methods from its
superclass and may also add new data fields and methods.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 4
8. Cont.
• The Circle class (Listing 11.2) extends the GeometricObject class
(Listing 11.1) using the following syntax:
• The keyword extends (lines 1–2) tells the compiler that the Circle
class extends the GeometricObject class, thus inheriting the methods
getColor, setColor, isFilled, setFilled, and toString.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 8
11. Using the super Keyword
• The keyword super refers to the superclass and can be used to invoke
the superclass’s methods and constructors.
• It can be used in two ways:
• To call a superclass constructor.
• To call a superclass method.
• The syntax to call a superclass’s constructor is:
• The syntax to call a superclass’s method is:
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 11
12. The Object Class and Its toString() Method
• Every class in Java is descended from the Object class.
• If no inheritance is specified when a class is defined, the superclass
of the class is Object by default.
• For example, the following two class definitions are the same:
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 12
13. Cont.
• It is important to be familiar with the methods provided by the Object
class so that you can use them in your classes.
• Here, we introduce the toString method in the Object class.
• The signature of the toString() method is:
• Invoking toString() on an object returns a string that describes the
object.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 13
14. Cont.
• By default, it returns a string consisting of
• a class name of which the object is an instance,
• an at sign (@),
• and the object’s memory address in hexadecimal.
• For example, consider the following code for the Loan class:
• The output for this code displays something like Loan@15037e5.
• This message is not very helpful or informative. Usually you should override the
toString method so that it returns a descriptive string representation of the object.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 14
15. Cont.
• For example, the toString method in the Object class was overridden
in the GeometricObject class in lines 46–49 in Listing 11.1 as
follows:
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 15
16. The Object’s equals Method
• Like the toString() method, the equals(Object) method is another useful
method defined in the Object class.
• Its signature is
• This method tests whether two objects are equal. The syntax for invoking
it is:
• The default implementation of the equals method in the Object class is:
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 16
17. Polymorphism
• Polymorphism means that a variable of a supertype can refer to a
subtype object.
• A class defines a type. A type defined by a subclass is called a
subtype, and a type defined by its superclass is called a supertype.
• Therefore, you can say that Circle is a subtype of GeometricObject
and GeometricObject is a supertype for Circle.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 17
19. Summary
• Inheritance is an important and powerful feature for reusing
software.
• A superclass is also referred to as a parent class or a base class.
• A subclass as a child class, an extended class, or a derived class.
• The super keyword can be used in two ways:
• To call a superclass constructor.
• To call a superclass method.
• Every class in Java is descended from the Object class.
• Polymorphism means that a variable of a supertype can refer to a
subtype object.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 19