Inheritance in java is a mechanism in which one object acquires all the properties and behavior of parent class.
By inheritance, reusability of code is achieved. In java inheritance is provided in two ways: by using classes and by using interfaces.
By using Java class the following types of inheritance is possible
we will discuss these three things here
By using java interfaces
1. Single inheritance
If a sub class is derived from only one super class, this is called single inheritance.
Example:
class A
{
int a=10;
void display()
{
System.out.println(a);
}
}
class B extend A
{
void show()
{
System.out.println(a);
}
}
class singleDemo
{
public static void main(String args[])
{
B b1=new B();
b1.display();
b1.show();
}
}
Explanation:
class A
{
void display()
{
System.out.println("first super class");
}
}
class B extend A
{
void show()
{
System.out.println("Second super class");
}
}
class C extends B
{
public static void main(String args[])
{
C c1=new B();
c1.display();
c1.show();
}
}
Explanation:
class A
{
int a=10;
void display()
{
System.out.println(a);
}
}
class B extend A
{
void show()
{
System.out.println(a);
}
}
class C extend A
{
public static void main(String args[])
{
B b1=new B();
b1.display();
C c1 = new C1();
c1.show();
}
}
Explanation:
The two derived classes B and C inherits a single base class A.
By inheritance, reusability of code is achieved. In java inheritance is provided in two ways: by using classes and by using interfaces.
By using Java class the following types of inheritance is possible
- Single level inheritance
- Multi level inheritance
- Hierarchical inheritance
we will discuss these three things here
By using java interfaces
- Multiple inheritance
- Hybrid inheritance.
1. Single inheritance
If a sub class is derived from only one super class, this is called single inheritance.
Example:
class A
{
int a=10;
void display()
{
System.out.println(a);
}
}
class B extend A
{
void show()
{
System.out.println(a);
}
}
class singleDemo
{
public static void main(String args[])
{
B b1=new B();
b1.display();
b1.show();
}
}
Explanation:
In the above program I declared two class 'A' and 'B'. Class A consist of one variable 'a' and one method display(). The class B inherits the properties of class A using 'extend keyword'. So, class B consists of one variable 'a' and two methods: display() and show() both are used to display the value of 'a'.
I declared again another class called 'singleDecom' for creating main method. Inside main method, Object is created for class B. Since in inheritance process object is created for derived class only.
2. Multilevel inheritance
A common requirement in object oriented programming is the used of a derived class as a super class. Java supports this concept and use it extensively in building class library. This concept allows us to build a chin of classes.
{
void display()
{
System.out.println("first super class");
}
}
class B extend A
{
void show()
{
System.out.println("Second super class");
}
}
class C extends B
{
public static void main(String args[])
{
C c1=new B();
c1.display();
c1.show();
}
}
Explanation:
In above program, Class A is inherited by class B, and then class B is inherited by class C. That means all the features of class A and class B are available in class C. So, I created object to class C and called two super classes methods.
3. Hierarchical Inheritance
Derivation of several classes from a single base class that is one class may be inherited by more than one class is called hierarchical inheritance.
{
int a=10;
void display()
{
System.out.println(a);
}
}
class B extend A
{
void show()
{
System.out.println(a);
}
}
class C extend A
{
public static void main(String args[])
{
B b1=new B();
b1.display();
C c1 = new C1();
c1.show();
}
}
Explanation:
The two derived classes B and C inherits a single base class A.
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.