Java inheritance. If you want to get knowledge and clear understanding
about Inheritance in Java. Then its a best place for you, here
i will teach you in detail about Java inheritance with very
easy to understand examples.
Inheritance in Java
In Java programming
language, sometimes we need to create a class based on already existing class
in order to get the features already present in the existing class. This
concept is known as Inheritance. Inheritance saves lots of time because
programmer can create new class with respect to another class and in this way
we can manage our application easily. Program readability also becomes good
using inheritance
Whenever we create a class
based on already existing class in Java, that new class is known as child class
or sub class or derived class. The class which provides its features including
data member as well as function members is known as Parent class or base class
or super class.
How to perform inheritance in Java
In order to perform
inheritance in Java, we need a keyword known as extends
Syntax
Class ChildClass extends
ParentClass
{
//code here
}
Example
Class A
{
Some code here
}
Class B extends A
//inheritance using extends keyword
{
Code here
}
In the above example, we are
extending A class using Java extends keyword. So B class will become child and
A class will be parent or super class.
Example Program
Class A
{
int a = 10;
}
Class B extends A
{
int b
= 20;
public
static void mian (String ar[ ] )
{
B
obj = new B(); //creating object of class B
System.out.println(obj.a);
//printing the result on console
System.out.println(obj.b);
//printing the result on console
}
}
Output will be:
10
20
In the above example, we
have two classes. One class is extending another class, so class B will be
child class and A class will be parent class. When we made the object of class
B which is acting as child class, his object (obj) can access his own data
member as well as parent class data member. That’s why we are getting out as 10
and 20.
Different types of Java Inheritance
We have following types of
Inheritance in Java language
1.
Single inheritance
2.
Multilevel
inheritance
3.
Hierarchical
inheritance
4.
Multiple inheritance
5.
Hybrid inheritance
Multiple Inheritance in Java
cannot be done with the help of classes because we cannot extends more than one
class in Java. But it is possible with the help of Java interface. So if we
talk about inheritance types in Java language, we can perform single,
multilevel and hierarchical inheritance only with the help of classes and remaining
can be possible using Java interface.
Single Inheritance
In Java, single inheritance
means one class is inheriting another class and its very simple inheritance. We
have just one child and one parent
For Example
Class A
{
void
ftn1()
{
Systemout.println(“Class
A member”);
}
}
Class B extends A //here
class B is the child class because it is extending class A
{
Void ftn2()
{
System.out.println(“Class B member”);
}
}
Class C
{
Public static void main (String ar[ ] )
{
B obj = new B(); //creating object of the child class
Obj.ftn2();
Obj.ftn1();
}
}
Output will be
Class
B member
Class
A member
In
the above example, class A is the parent class and class B is the child class,
because, it is inheriting the features of parent class. We have one child and
one parent class. After that we have a new class which contains main method and
we have created an object here of class B and accessing the function member of
class B as well as class A.
Java Multilevel Inheritance
In
this inheritance, we have different classes and they are extending each other
to make a chain. For example, we may have a class A, class B and class C. Class
A is the parent of class B and class B is the parent of class C and so on
Example
Program
Class
A
{
Void ftn1()
{
System.out.println(“class A member”);
}
}
Class
B extends A //inheriting class A from class B
{
Void ftn2()
{
System.out.println(“class B member”);
}
}
Class C extends B //inheriting class B using class C
{
Void ftn3()
{
System.out.println(“class C member”);
}
}
Class
Multi
{
Public static void main (String a[ ])
{
C obj = new C();
Obj.ftn3();
Obj.ftn2();
Obj.ftn1();
}
}
Output
will be:
Class
C member
Class
B member
Class
A member
Hierarchical Inheritance in Java
In
Java, hierarchical inheritance is possible when two classes or more than
classes try to inherit a class, i.e. multiple classes have one parent class, it
is known as hierarchical inheritance. For example, we have a class A, B, C
where A is the parent class of class B and class C
Example
Program
Class
A
{
Void
ftn1()
{
System.out.println
(“class A function”);
}
}
Class
B extends A
{
Void
ftn2()
{
System.out.println
(“class B function”);
}
}
Class
C extends A
{
Void
ftn3()
{
System.out.println
(“class C function”);
}
}
Class
HeirarchicalInheri
{
Public static void main (String arg[ ])
{
C obj = new C();
Obj.ftn3();
Obj.ftn1();
}
}
Output
will be:
Class
C function
Class
A function
Multiple
Inheritance is not possible in Java using Classes because it can create issue when
calling functions. To explain it fully, let’s consider an example
Class
First
{
Void
ftn1()
{
System.out.println(“First
class method”);
}
}
Class
Second
{
Void
ftn1()
{
System.out.println(“Second
class method”);
}
}
Class
Third extends First,Second //its actually not possible in Java and you will get
error here
{
Pubic static void main (String arg[ ])
{
Third obj = new Third();
Obj.ftn1(); //here we have issue because both
classes (First and Second) have ftn1() method and as Third class is extending
both classes, so Java will have no idea which ftn1() of the classes should be
called by the object
}
}
Output
will be:
Compile
time error
Solution to Multiple Inheritance Problem
We
can solve this multiple inheritance problem in Java using interfaces which is a
better approach and compiler will not have any confusion when calling methods

No comments:
Post a Comment