0% found this document useful (0 votes)
6 views

Java

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Inheritance in

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

1 The topmost class in the hierarchy. It represents a general


concept.

Intermediate Classes
Classes inheriting from other classes and potentially being
2
inherited by further subclasses, forming the branches of
the hierarchy.

Leaf Classes

3 The classes at the bottom, representing more specialized


concepts. They inherit from the classes above them.
Types of Inheritance in
Java

1 Single Inheritance 2 Multilevel


A subclass inherits from
Inheritance
only one superclass, A subclass inherits from a
promoting clarity and superclass, which itself
simplicity in the class inherits from another
hierarchy. superclass, forming a chain
of inheritance.

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

Default Yes Yes Yes (if in No


same
package)

Protected Yes Yes Yes No

Public Yes Yes Yes Yes


Method Overriding
Method overriding is a key feature in Java that allows a subclass to
provide a specific implementation for a method that is already defined in
its superclass. The overridden method in the subclass must have the same
name, return type, and parameters as the method in the superclass.
Calling Superclass
Constructors
Superclass Constructor First
1
When a subclass object is created, the constructor of its
superclass is invoked first.

Then Subclass Constructor


2
After the superclass constructor completes, the
subclass's constructor is executed to initialize subclass-
specific members.
The `super` Keyword

1 Accessing 2 Calling Superclass


Superclass Constructors
Members Invoke superclass
Use `super` to access constructors using
superclass members, `super()` to ensure proper
especially when overridden initialization of inherited
by the subclass. members.
Abstract Classes and Inheritance
Abstract Classes Concrete Subclasses

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.

You might also like