0% found this document useful (0 votes)
4 views19 pages

5 Inheritance

The document outlines the concepts of inheritance in object-oriented programming (OOP), detailing the relationships between superclasses and subclasses, and the advantages of using inheritance to reduce code duplication and improve maintainability. It explains how subclasses inherit attributes and methods from superclasses, the significance of method overriding, and the behavior of constructors in subclasses. The content is structured for a course on Object Oriented Programming at the Vietnam National University of HCMC, focusing on practical examples and Java syntax.

Uploaded by

vhqubao
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)
4 views19 pages

5 Inheritance

The document outlines the concepts of inheritance in object-oriented programming (OOP), detailing the relationships between superclasses and subclasses, and the advantages of using inheritance to reduce code duplication and improve maintainability. It explains how subclasses inherit attributes and methods from superclasses, the significance of method overriding, and the behavior of constructors in subclasses. The content is structured for a course on Object Oriented Programming at the Vietnam National University of HCMC, focusing on practical examples and Java syntax.

Uploaded by

vhqubao
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

Vietnam National University of HCMC

International University
School of Computer Science and Engineering

Object Oriented Programming


Inheritance
Instructor: Le Thi Ngoc Hanh, Ph.D
ltnhanh@[Link]
Semester – Spring 2026
VNU-HCM International University School of Computer Science and Engineering

Lecture Objectives

 Concept of inheritance and its purpose in OOP

 Superclass and subclass relationships

 Controlled access in Inheritance

 Constructors in subclasses

Slide 2 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Example
In many software systems, different objects share
common attributes and behaviors.

Slide 3 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Problem without Inheritance


 Code duplication
Multiple classes contain identical implementations.

 Maintenance difficulties
A change in deposit logic must be updated in several classes.

 Higher probability of errors


Different classes may accidentally implement the same method
differently.

Object-oriented design introduces inheritance to solve this


problem.

Slide 4 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Definition of Inheritance
 Inheritance is a mechanism in object-oriented programming
where a new class derives properties and behaviors from an
existing class.
▪A general class defines common attributes and behaviors.
▪ More specialized classes extend this general class and inherit its
features.
 Advantages: promotes code reuse; reduces redundancy;
Improves maintainability and extensibility of software systems
 Inheritance establishes logical hierarchical relationships
between classes

Slide 5 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Inheritance
 Objects in the real world and in mathematics can
be further classified by hierarchical taxonomies.
 For the taxonomy X

we say “every Y is-a X”. For example,


Cat

Kitten

“every kitten is a cat”


Slide 6 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering

Taxonomy
A taxonomy is a hierarchical
classification where:
▪ Eachsubclass belongs to exactly
one parent category
▪ The relationships represent IS-A
relationships
▪ The hierarchy forms a tree structure
▪A child node should have only one
parent

Slide 7 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Taxonomy

Slide 8 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Superclasses and Subclasses


In an inheritance hierarchy, two important terms are used:

 Superclass: A superclass (also called parent class or base class)


is a class that contains common attributes and methods
shared by other classes.

 Subclass: A subclass (also called child class or derived class) is


a class that inherits attributes and methods from a
superclass.

 The subclass can: use inherited methods; define additional


methods; modify inherited behavior

Slide 9 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Class Hierarchy Example


 Inheritance should always represent logical
specialization relationships.
BankAccount

SavingAccount CheckingAccount

→ This relationship is known as an IS-A relationship.

Slide 10 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Java Syntax for Inheritance


In Java, inheritance is implemented using the keyword “extends”.
Syntax:

Example:

 The class SavingsAccount inherits all accessible members of the


class BankAccount.

 This allows the subclass to reuse code defined in the superclass.

Slide 11 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Example of a Superclass

Slide 12 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Example of a Subclass

Slide 13 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

What Does a Subclass Inherit?


 When a subclass extends a superclass, it inherits the
accessible members of the superclass.
▪ Inherited members include:
▪ fields (instance variables)
▪ methods

 However, some elements are not inherited:


▪ constructors

▪ private members
▪ Private members exist within the superclass but cannot be directly
accessed by the subclass.
Slide 14 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering

Inheritance - UML

Slide 15 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Relationship Between Superclass and Subclass


 A subclass interacts with the superclass in several
ways:
▪ Use inherited methods

▪ Add new methods

▪ Override inherited methods

A subclass may redefine a method to provide


specialized behavior.

Slide 16 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Method Overriding
 Method overriding occurs when a subclass
provides its own implementation of a method
defined in the superclass.
 Notation @Override indicates that the method
overrides a superclass method.

Slide 17 OOP – Spring 2026


VNU-HCM International University School of Computer Science and Engineering

Constructors in Subclasses
 Constructors behave differently from ordinary methods in an inheritance
hierarchy. A subclass does not inherit the constructors of its superclass, but it
must still ensure that the superclass portion of the object is properly
initialized.

 When an object of a subclass is created, Java follows a constructor


execution chain:

▪ The constructor of the superclass is executed first.

▪ After the superclass constructor finishes, the constructor of the subclass executes.

This order ensures that all inherited attributes are initialized before the
subclass performs its own initialization.
Slide 18 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering

Sample code

Slide 19 OOP – Spring 2026

You might also like