Multilevel Inheritance in java

Ads
 

Multilevel Inheritance in java

We are going to discuss about multilevel inheritance in java.

We are going to discuss about multilevel inheritance in java.

Multilevel Inheritance in java

We are going to discuss about multilevel inheritance in java. The Multilevel inheritance is inherit of classes more than two classes. We use multilevel inheritance to create one-to-one ladder. "super" keyword is used to inherit features of a superclass into subclass in Multilevel inheritance. We can access properties of all classes. we have used extend keyword, extend keyword is a inherit  parent class to subclass. Java, every class is a subclass of  java.lang.Object.

Example of Multilevel Inheritance in java

package Inheritance;
class Animal
{
        private int cow;
        private String bear;
        public void setstud(int no,String name)
        {
        	cow = no;
        	bear = name;
        }
        public void putstud()
        {
                System.out.println("Eat of Animal milk: " + cow);
                System.out.println("Eat of Animal gras : " + bear);
        }
}
class test extends Animal
{
        protected int test1,test2;
        public void settest(int k,int l)
        {
        	   test1 = k;
                test2 = l;
        }
        public void puttest()
        {
                System.out.println("test1 : " + test1);
                System.out.println("test2 : " + test2);
        }
}
class MultilevelExample extends test
{
        private int total;
        public void calc()
        {
                total = test1 + test2;
        }
        public void puttotal()
        {
               System.out.println("Total : " + total);
        }
        public static void main(String args[])
        {
        	MultilevelExample f = new MultilevelExample();
                f.setstud(100,"Nithya");
                f.settest(78,89);
                f.calc();
                f.putstud();
                f.puttest();
                f.puttotal();
        }
}

OutPut

Download Source Code

Ads

Ads