Java inheritance and programming example?

 



 Java inheritance and programming example

What Is inheritance?

·         It is inheriting the properties of parent class into child class. inheritance is the procedure by which one object acquires all the properties and behavior of the parent object.

Important points of  inheritance:--

1.       inheritance is an achieve  by using the “extends “ keyword

2.       every class has a super o say parent class i.e object class (but object class does not have any parent class).

Inheritance advantage:--

1.       Code reusability

2.       We can archive polymorphism using the inheritance method overriding.

Type of inheritance:-

Java can achive 3 type inheritance  single inheritance,  multilevel inheritance and hierarchical inheritance.

       

Disadvantage  of inheritance:-

Using inheritance the two classes (parent child class) gets tightly coupled.

Single inheritance:--   single inheritance  one class extends another class(one class only).

Multilevel inheritance:-    multilevel inheritance one class can inherit from a derived class. Hence the derived  class becomes the base class for the new class.

Hierarchical inheritance:--    in hierarchical inheritance  one class is inherited by many sub class.

Multiple inheritance:-- in multiple inheritance one class extends more than one classes.java does not support multiple inheritance.

Hybrid inheritance:--    hybrid inheritance is a combination of any two inheritance. Java does not support hybrid inheritance.

Below does not take part inheritance :-

Constructor:-      a sub –class inherit all the member (fields, method, and nested class) form its super class .constructor are not members so they are not inherited by sub-classes . but the constructor of the super class can be invoked from the sub-class.

Privet members:--     a sub class  does not inherit the privet member of its parent class. How ever if the super class has public or protected method(like getter and setter ) for accessing its privet fields .these can also be used by the sub classes.

                                  There can be only one super class. not more than that because java does not support multiple inheritance.

Programming example of single inheritance:--

class    SingleInheritance       {

void    show()   {

System.out.println(“parent class”);

}

}

class  Child   extends SingleInheritance       {

void   show1()  {

System.out.println(“child class”);

}

public static void amin(String   args[])   {
Child   obj  =  new Child();

obj .show();

obj .show1();

}
}

Programming example of multilevel inheritance:--

class    MultilevelInheritance       {

void    show()   {

System.out.println(“parent class”);

}

}

class  Child   extends  MultilevelInheritance       {

void   show1()  {

System.out.println(“child class”);

}

class  Sub_Child   extends  Child       {

void   show2()  {

System.out.println(“sub-child class”);

}

public static void amin(String   args[])   {
Sub_Child obj  =  new Sub_Child ();

obj .show();

obj .show1();

obj .show2();

}
}

Programming example of hierarchical inheritance:--

class    HierarchicalInheritance       {

void    show()   {

System.out.println(“parent class”);

}

}

class  Child   extends  HierarchicalInheritance       {

void   show1()  {

System.out.println(“child class”);

}

class  Sub_Child   extends  HierarchicalInheritance       {

void   show2()  {

System.out.println(“sub-child class”);

}

public static void amin(String   args[])   {
Sub_Child obj  =  new  Sub_Child ();

obj .show2();

}
}



 

 

Post a Comment

0 Comments