Chapter4-Inheritance
Chapter4-Inheritance
Inheritance promotes code reuse and enables the creation of more specialized
classes based on existing classes.
By BM
2
Introduction 4/4/2025
In procedural programming, data and operations on the data are separate.
In procedural programming, data and operations on the data are separate.
In procedural programming, data and operations on the data are separate.
Functionality of Inheritance
Base classes (ancestor classes) provide attributes and behaviors to derived classes.
Inheritance also embodies the Is-A relationship. This is easier to understand with
real-world examples. For instance, “A Dog is an Animal” makes sense, with
'Animal' being the parent class and 'Dog' as the child class. You can visualize
this idea through a diagram that illustrates how inheritance works in practice.
}}
4/4/2025
OOP Chapter 3 [by Haftu M. & Bekalu M.] 8
Types of inheritance
There are different types of Single Inheritance:
• Single inheritance is a type of inheritance by which
inheritance, the commonly
A single child class extends from only a single
used types of inheritance are:
parent class. The diagram shown below illustrates
Single Inheritance a single inheritance, Class A is the parent class and
Multilevel Inheritance the Class B is the child class, where Class B only
extends from Class A.
Multiple Inheritance
Hierarchical Inheritance
On the other hand, the class that inherits all the members (fields, methods, and nested
classes) from another class is called the subclass. In simpler terms, a newly created class is a
subclass. It can also be referred to as a derived class, child class, or extended class.
To inherit from a class, you use the extends keyword. This keyword signifies that the new class
is extending the functionality of an existing class.
Subclasses don’t inherit the constructor of the subclass but rather they can invoke using the
super keyword.
In Java, access modifiers are keywords that define the scope and visibility of classes, methods,
variables, and constructors. These modifiers help specify where and how the elements of your code
can be accessed, making your code more organized and secure.
Access modifiers are a fundamental part of object-oriented programming because they enforce
encapsulation, a core principle that restricts direct access to some components of an object. By doing
so, they help protect the integrity of your objects and ensure that their internal workings are hidden
from the outside world, allowing for safer and more manageable code.
}}