Chapter Four:
Inheritance
Abey B.(MSc)
Debre Berhan University(DBU)
Faculty of Computing
Department Of Information System
December 11, 2024
Abey B. (DBU) Java Programming December 11, 2024 1 / 35
What is Inheritance
Inheritance is an important pillar of OOP(Object-Oriented
Programming).
Inheritance in Java is a concept that acquires the properties from
one class to other classes
Inheritance means creating new classes based on existing ones.
Inheritance is the mechanism in Java by which one class is allowed
to inherit the features(fields and methods) of another class.
Abey B. (DBU) Java Programming December 11, 2024 2 / 35
What is Inheritance
A class that inherits from another class can reuse the methods
and fields of that class.
▶ In addition, you can add new fields and methods to your current
class as well.
In Java, the idea of inheritance means that new classes can be
built on top of existing ones.
The class that inherits the properties is known as the sub-class (
child class or derived class.)
The class from which the properties are inherited is known as the
superclass (the parent class or base class.)
Abey B. (DBU) Java Programming December 11, 2024 3 / 35
What is Inheritance
A subclass is a specialized version of a superclass
▶ It inherits all of the instance variables and methods defined by the
superclass and add its own, unique elements.
Inheritance represents the IS-A relationship which is also known
as a parent-child relationship.
Terms used in Inheritance
▶ Super class and base class are synonyms of Parent class.
▶ Sub class and derived class are synonyms of Child class.
▶ Properties and fields are synonyms of Data members.
▶ Functionality is a synonym of method
Abey B. (DBU) Java Programming December 11, 2024 4 / 35
What is Inheritance
In Java, we have two types of relationship:
1 Is-A relationship:
2 Has-A relationship:
Abey B. (DBU) Java Programming December 11, 2024 5 / 35
What is Inheritance
What is Is-A relationship
Whenever one class inherits another class, it is called an IS-A
relationship.
Represents a type of relationship between two classes where one
class is a specialized version of another.
It implies that a subclass is a specific type of its superclass.
Example
▶ If a class named Mango inherits another class named Fruit,
⋆ Then we can say that Mango is having is-a-relationship with Fruit,
which implies Mango is a Fruit.
Abey B. (DBU) Java Programming December 11, 2024 6 / 35
What is Inheritance
What is Is-A relationship
Inheritance is uni-directional.
For example,
▶ House is a Building. But Building is not a House.
Abey B. (DBU) Java Programming December 11, 2024 7 / 35
What is Inheritance
What is HAS-A Relationship
Whenever an instance of one class is used in another class, it is
called HAS-A relationship.
In simple terms, it indicates that an instance of one class has a
reference to another class’s instance or another instance of the
same class. For Example
▶ Maruti has Engine, or House has Bathroom.
Abey B. (DBU) Java Programming December 11, 2024 8 / 35
What is Inheritance
What is HAS-A Relationship
Abey B. (DBU) Java Programming December 11, 2024 9 / 35
What is Inheritance
What is extends Keyword?
extends is the keyword used to inherit the properties of a class.
The syntax of Java Inheritance
Abey B. (DBU) Java Programming December 11, 2024 10 / 35
What is Inheritance
Example 1
Abey B. (DBU) Java Programming December 11, 2024 11 / 35
What is Inheritance
The Output of Example 1 is
Abey B. (DBU) Java Programming December 11, 2024 12 / 35
What is Inheritance
Example 2
Abey B. (DBU) Java Programming December 11, 2024 13 / 35
What is Inheritance
The Output of Example 2 is
Abey B. (DBU) Java Programming December 11, 2024 14 / 35
What is Inheritance
Example 3
Abey B. (DBU) Java Programming December 11, 2024 15 / 35
What is Inheritance
The Output of Example 3 is
Abey B. (DBU) Java Programming December 11, 2024 16 / 35
Types of Access Modifiers
Protected Access Modifier
Abey B. (DBU) Java Programming December 11, 2024 17 / 35
What is Inheritance
Access modifiers in OOP
Access Modifiers (Access Specifiers) are keywords that are used
in OOP (object-oriented programming) in order to specify the
accessibility of the methods, classes, constructors, and other
members of the class.
They tell the compiler which class or method should have the
access to the data or method being defined.
Abey B. (DBU) Java Programming December 11, 2024 18 / 35
What is Inheritance
Why do we use it?
Access Modifiers are mainly created for the purpose of
Encapsulation
Encapsulation or Data Hiding is a method in object-oriented
programming where the values of the variables or the state of the
data object are kept hidden or enclosed inside a class.
In short, Access Modifiers are used to define who does or who does
not have access to certain features in the class and methods of
a program.
Abey B. (DBU) Java Programming December 11, 2024 19 / 35
Types of Access Modifiers
There are two types of modifiers in Java:
▶ Access modifiers
▶ Non-access modifiers.
Abey B. (DBU) Java Programming December 11, 2024 20 / 35
What is Inheritance
Types of Access Modifiers
There are a number of different Access Modifiers depending on
the programming language.
In Java, we have 4 Access Specifiers
1 Default
2 Public
3 Private
4 Protected
Abey B. (DBU) Java Programming December 11, 2024 21 / 35
What is Inheritance
Types of Access Modifiers
Abey B. (DBU) Java Programming December 11, 2024 22 / 35
Types of Access Modifiers
Default
When no access modifier is specified for a class, method, or data
member
▶ It is said to be having the default access modifier by default.
▶ We can access the default modifier only within the same
package and not from outside the package.
▶ if we do not specify any access modifier it will automatically
consider it as default.
▶ It is less restrictive than private but more restrictive than
protected and public.
Abey B. (DBU) Java Programming December 11, 2024 23 / 35
Default
Example
Abey B. (DBU) Java Programming December 11, 2024 24 / 35
Types of Access Modifiers
Private Access Modifier
The private access modifier is specified using the keyword private.
The methods or data members declared as private are accessible
only within the class in which they are declared.
Any other class of the same package will not be able to access
these members.
private means “only visible within the enclosing class”.
It provides more accessibility than the default modifer.
Abey B. (DBU) Java Programming December 11, 2024 25 / 35
Types of Access Modifiers
Private Access Modifier
Abey B. (DBU) Java Programming December 11, 2024 26 / 35
Types of Access Modifiers
Protected Access Modifier
The protected access modifier is specified using the keyword
protected.
The methods or data members declared as protected are
accessible within the same package or subclasses in different
packages.
The protected access modifier is accessible within package and
outside the package but through inheritance only.
The protected access modifier can be applied on the data member,
method and constructor.
▶ But,It can’t be applied on the class.
Abey B. (DBU) Java Programming December 11, 2024 27 / 35
Types of Access Modifiers
Protected Access Modifier
Abey B. (DBU) Java Programming December 11, 2024 28 / 35
Types of Access Modifiers
Public Access Modifier
The public access modifier is specified using the keyword public.
The public access modifier has the widest scope among all other
access modifiers.
The public access modifier is accessible everywhere.
It has the widest scope among all other modifiers.
Abey B. (DBU) Java Programming December 11, 2024 29 / 35
Types of Access Modifiers
Public Access Modifier
Abey B. (DBU) Java Programming December 11, 2024 30 / 35
Types of Access Modifiers
Non-Access Modifiers
For classes, you can use either or :
1 final
2 abstract
For attributes and methods the most common
▶ final
▶ static
▶ abstract
Abey B. (DBU) Java Programming December 11, 2024 31 / 35
Non-Access Modifiers
For classes
final
▶ The class cannot be inherited by other classes
abstract
▶ The class cannot be used to create objects
▶ To access an abstract class, it must be inherited from another class.
Abey B. (DBU) Java Programming December 11, 2024 32 / 35
Non-Access Modifiers
For attributes and methods
final
▶ Attributes and methods cannot be overridden/modified
static
▶ Attributes and methods belongs to the class, rather than an object
abstract
▶ Can only be used in an abstract class, and can only be used on
methods.
▶ The method does not have a body
Abey B. (DBU) Java Programming December 11, 2024 33 / 35
Types of Access Modifiers
Example for Final
Abey B. (DBU) Java Programming December 11, 2024 34 / 35
Types of Access Modifiers
Example for static
Abey B. (DBU) Java Programming December 11, 2024 35 / 35