Publish your story - poem - jokes free online--click here

JAVA

Inheritance of Class in Java

Inheritance: -- It is a mechanism by which we may access the property (variable and methods) of a class in another class.

Base /Super/Parent class: -- The class which inherits other class (in other words class whose property goes to other class). E.g. à your father is a base class with respect to you.

Derived / Sub / Child class: -- The class which gets inherited from other class (in other words class who gets the property of other class). E.g. à you are a derived class with respect to your father.

Example of Inheritance
class base
{
                void show()
                {
                                System.out.println("this is base class method");
                }
}
class derived extends base //derived is getting inherited by base class
{
                void disp()
                {
                                System.out.println("this is derived class method");
                }
}
public class abc
{
                public static void main(String as[])
                {
                                derived d=new derived(); //object of derived class
                                d.show(); //calling method of base class by derived class object
                                d.disp(); //method of derived class
                }
}
Types of Inheritance
JAVA supports three types of Inheritance
1>> Single Inheritance
2>> Multilevel Inheritance
3>> Hierarchical Inheritance

Single Inheritance
When only one class inherits the only one derived class it is called single inheritance.
Number of base class ==== =only one
Number of derived class===only one




Multilevel Inheritance
When inheritance is done in more than one level is called multi level.
Number of base class ======= more than one
Number of derived class ====  more than one

Example of multilevel inheritance
class base
{
                void show()
                {
                                System.out.println("this is base class method");
                }
}
class semiderived extends base //one level of inheritance
{
                void disp()
                {
                                System.out.println("this is semiderived class");
                }
}

class derived extends semiderived //second level inheritance
{
                void display()
                {
                                System.out.println("this is derived class method");
                }
}
public class abc
{
                public static void main(String as[])
                {
                                derived d=new derived();
                                d.show();
                                d.disp();
                                d.display();
                }
}

Hierarchical Inheritance
When only one base class inherits the more than one derived class it is called. Hierarchical Inheritance
Number of base class======= only one
Number of derived class===== more than one

Example of Hierarchical Inheritance
class base
{
                void show()
                {
                                System.out.println("this is base class method");
                }
}
class derived1 extends base //first derived class
{
                void disp()
                {
                                System.out.println("this is derived1 class");
                }
}

class derived2 extends base //second derived class
{
                void disp()
                {
                                System.out.println("this is derived2 class method");
                }
}
public class abc
{
                public static void main(String as[])
                {
                                derived1 d1=new derived1();
                                d1.show();
                                d1.disp();
                                derived2 d2=new derived2();
                                d2.show();
                                d2.disp();
                }
}


No comments:

Post a Comment