Inheritance in JAVA
Inheritance in Java is
a mechanism in which one
object obtains all the characteristics and behaviors of
a parent object. It is an important part of OOPs (Object Oriented programming
system).
The idea behind inheritance in Java is that you can create new classes that are
dependent upon existing classes. When you inherit from an existing class, then
you are able to reuse methods and
attributes of the parent class. Moreover, you can
add new
methods and
attributes in your current class too.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Child Class:
The class that extends the features of another class is known as child class,
sub class or derived class.
Parent Class:
The class whose properties and functionalities are used(inherited) by another
class is known as parent class, super class or Base class.
Why use inheritance in java
Reusibility of the code -
It allows
us to reuse of the code. it
improves reusability in java application.The
biggest advantage of Inheritance is that the code that is already written in parent
class need not be rewritten in the child class.
The syntax of Java Inheritance
-
class Childclass-name extends Parentclass-name
-
{
-
-
}
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java:
single, multilevel and hierarchical.
Single Inheritance Example
When a class inherits another class, it is known as a single
inheritance.
Multilevel Inheritance Example
When there is a chain of inheritance, it is known as multilevel
inheritance.
Hierarchical Inheritance Example
When two or more classes inherits a single class, it is
known as hierarchical inheritance.
|