Inheritance in Java
Inheritance
Inheritance is one of the key feature of Object Oriented
Programming in java. It is a mechanism in which one object acquires all the
properties and behaviors of parent object. Inheritance represents the IS-A
relationship, also known as parent-child relationship. The
derived class inherits the states and behaviors from the base class. The
derived class is also known as sub class and base class is also known as
super-class.
Inheritance is used in Java to achieve Method Overloading
(To achieve runtime polymorphism) and Code Reusability.
Syntax of Inheritance:
class Subclass-name extends Superclass-name
{
//methods and fields
}
Extends keyword represents that you are making a new class
which will extend the features of super class.
1.
Superclass-name is super class of Subclass-name.
2.
Subclass-name is sub class of Superclass-name.
3.
Subclass-name IS-A Superclass-name.
Disadvantage of Inheritance is that if you need to change
code of parent class it will affect to all the child classes which are
extending parent class, it cannot be independent of each other which is also known
as Tightly Coupled.
Types of Inheritance
in java:
1.
Single Inheritance.
2.
Multilevel Inheritance.
3.
Hierarchical Inheritance.
Note: Multiple
inheritance is not supported in Java through classes but we can achieve this
feature using Interface in java.
It is not supported in java to reduce
complexity and simplify the language. To provide more maintainable and clear
design.Consider a scenario
where you have classes A,B and C. Class C inherits Class A and B if both
classes(A & B) have same method and you call it from child class object,
there will be ambiguity to call method of A and B class.
Comments
Post a Comment