Inheritance in Java
π§ What is Inheritance?
Inheritance is one of the core features of OOP. It allows a child class to inherit the properties and behaviors (fields and methods) of a parent class.
πΈ Types of Inheritance:
-
Single β One subclass, one superclass.
-
Multilevel β Class inherits from a subclass of another.
-
Hierarchical β Multiple classes inherit from one class.
β Java doesnβt support multiple inheritance directly using classes.
β Syntax:
π§ͺ Usage:
π Benefits:
-
Promotes code reuse.
-
Helps in method overriding.
-
Supports runtime polymorphism.
π Key Points:
-
extends
keyword is used. -
The child class inherits all non-private members.
-
Constructors are not inherited, but the parent constructor can be called using
super()
.