Classification and Inheritance

Classification and Inheritance

Previous Home Next

 

Basically java support four type of inheritance:-
1.Single inheritance
2.Multilevel inheritance
3.Hierarchical inheritance
4.Hybrid inheritance

Single inheritance:-When a  subclass is derived simply from it's parent class then this mechanism is known as simple inheritance. In case of simple inheritance there is only a sub class and it's parent class.

Multilevel inheritance:-
It is the enhancement of the concept of inheritance. When a subclass is derived from a derived class then this mechanism is known as the multilevel inheritance.

Hierarchical inheritance:- when child class inherit the feature of parent class then it is called the hierarchical inheritance

Hybrid inheritance:- Hybrid inheritance inherit the features of two classes like a hybrid contain the feature of two .similarly hybrid inheritance inherit the features of two classes.
 


 Single inheritance
class A
{
int x;
int y;
int get(int p, int q)
{
x=p; y=q; return(0);
}
void Show()
{
System.out.println(x);
}
}

Multilevel inheritance


class A
{
int x;
int y;
int get(int p, int q)
{
x=p; y=q; return(0);
}
void Show()
{
System.out.println(x);
}
}
class B extends A
{
void Show()
{
System.out.println("B");
}
}

class C extends B
{
void display()
{
System.out.println("C");
}
public static void main(String args[]){
A a = new A();
a.get(6,9);
a.Show();
}
}




class B extends A
{
public static void main(String args[]){
A a = new A();
a.get(6,9);
a.Show();
}
void display()
{
System.out.println("B");
}
}


Previous Home Next