Java
Java
Java: Unlocking
Code Reusability
Inheritance is a fundamental pillar of object-oriented programming (OOP)
that allows classes to inherit properties and behaviors from other classes.
This powerful mechanism promotes code reusability, reduces
redundancy, and enhances code organization.
by Kannarapu Keerthana
What is Inheritance?
Concept Benefits
Inheritance in Java lets a class This relationship facilitates
(subclass) inherit features from code reuse, as subclasses can
another class (superclass). leverage existing code from
This promotes a hierarchical their superclasses, reducing
structure, much like a family development time.
tree.
Real-World Analogy
Think of a car inheriting basic attributes from the broader category of
"vehicles" - like having wheels and an engine.
Superclass and Subclass
Superclass (Parent Class) Subclass (Child Class)
The class being inherited from, providing its attributes and The class inheriting from the superclass. It gains access to the
methods to the subclass. superclass's non-private members and can also add its own
unique features.
Inheritance Hierarchy
Base Class
Intermediate Classes
Classes inheriting from other classes and potentially being
2
inherited by further subclasses, forming the branches of
the hierarchy.
Leaf Classes
3 Hierarchical Inheritance
Multiple subclasses inherit from a single superclass. This models a
"one-to-many" relationship.
Access Modifiers and
Inheritance
Access Within Within By Outside
Modifier Class Package Subclass Package
Private Yes No No No
Cannot be instantiated directly. They often define abstract Subclasses must provide concrete implementations for all
methods, acting as blueprints for subclasses. abstract methods inherited from the abstract superclass.