SlideShare a Scribd company logo
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
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
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
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
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 5
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 6
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 7
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
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 9
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 10
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
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
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
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
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
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
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
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 18
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
The End
Thank You
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 20

More Related Content

PPTX
Advanced Java - Lec #3 ~ Practical Work Examples.pptx
PPTX
Advanced Java - Lec #2 ~ Practical Work Examples.pptx
PPTX
chapter 5 concepts of object oriented programming
PDF
Chapter 7 & 8 Classes in Java and Functions.pdf
PPTX
Object Oriented Programming
PPTX
Adaptive Navigation Support and Open Social Learner Modeling for PAL
PDF
lecture1.pdf
Advanced Java - Lec #3 ~ Practical Work Examples.pptx
Advanced Java - Lec #2 ~ Practical Work Examples.pptx
chapter 5 concepts of object oriented programming
Chapter 7 & 8 Classes in Java and Functions.pdf
Object Oriented Programming
Adaptive Navigation Support and Open Social Learner Modeling for PAL
lecture1.pdf

Similar to Advanced Java - Lec #5 - Inheritance and Polymorphism.pptx (20)

PPTX
Object Oriented Programming Week 2 Lecture
PDF
Introduction to C++ Class & Objects. Book Notes
PPTX
CT077-3-2-Data Structures - 0 - Introduction and Module Overview (1).pptx
PPTX
Ch5 inheritance
DOC
C# by Zaheer Abbas Aghani
DOC
C# by Zaheer Abbas Aghani
PPTX
Moodle Analytic Admin Tool Plugin for Student Performance Predict
PPTX
Object Oriented Programming.pptx
PPTX
Lesson 13 object and class
PPTX
PPT Lecture-1.4.pptx
PPTX
Java Inheritance - sub class constructors - Method overriding
PDF
Web Design & Development - Session 9
PDF
Ed tech 522 syllabus
PPTX
Object oriented programming
PPTX
INHERITANCES.pptx
PPT
Project template
DOC
CMSC198 summer2012 syllabus
PPT
Java htp6e 09
PPTX
Building a Microblog Corpus for Search Result Diversification
PPTX
Wilczewski iced report to nasa at ksc on 20180609 f
Object Oriented Programming Week 2 Lecture
Introduction to C++ Class & Objects. Book Notes
CT077-3-2-Data Structures - 0 - Introduction and Module Overview (1).pptx
Ch5 inheritance
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
Moodle Analytic Admin Tool Plugin for Student Performance Predict
Object Oriented Programming.pptx
Lesson 13 object and class
PPT Lecture-1.4.pptx
Java Inheritance - sub class constructors - Method overriding
Web Design & Development - Session 9
Ed tech 522 syllabus
Object oriented programming
INHERITANCES.pptx
Project template
CMSC198 summer2012 syllabus
Java htp6e 09
Building a Microblog Corpus for Search Result Diversification
Wilczewski iced report to nasa at ksc on 20180609 f
Ad

Recently uploaded (20)

PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
PDF
STL Containers in C++ : Sequence Container : Vector
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PPTX
Custom Software Development Services.pptx.pptx
PDF
Website Design Services for Small Businesses.pdf
PDF
Types of Token_ From Utility to Security.pdf
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PPTX
"Secure File Sharing Solutions on AWS".pptx
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
PPTX
GSA Content Generator Crack (2025 Latest)
PDF
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
Time Tracking Features That Teams and Organizations Actually Need
Designing Intelligence for the Shop Floor.pdf
Autodesk AutoCAD Crack Free Download 2025
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
STL Containers in C++ : Sequence Container : Vector
Oracle Fusion HCM Cloud Demo for Beginners
Custom Software Development Services.pptx.pptx
Website Design Services for Small Businesses.pdf
Types of Token_ From Utility to Security.pdf
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
"Secure File Sharing Solutions on AWS".pptx
How to Use SharePoint as an ISO-Compliant Document Management System
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
GSA Content Generator Crack (2025 Latest)
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Salesforce Agentforce AI Implementation.pdf
Time Tracking Features That Teams and Organizations Actually Need
Ad

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
  • 5. Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 5
  • 6. Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 6
  • 7. Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 7
  • 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
  • 9. Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 9
  • 10. Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 10
  • 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
  • 18. Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 18
  • 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
  • 20. The End Thank You Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 20

Editor's Notes

  • #3: Comprehend = درک کردن
  • #12: Descended = فرود آمد