Tuesday, 14 July 2015

INHERITANCE IN JAVA AND INHERITANCE WITH ONE EXAMPLE




Inheritance:
It is a concept provided in java which is used for extending the parent class stuff to the child class.
Note: one class can’t extend more than one classes at a time in java.
          one class can implement one or more interfaces at a time.


Develop a java program to produce the following o/p using inheritance

package pack1;

public class father {

          public void earn()
          {
                   System.out.println("logic for father earning");
          }
          public void drive()
          {
                   System.out.println("logic for father driving");
          }
         
public class son extends father {
public void drive() {
         
System.out.println("logic for son drive");
}
public void study() {
System.out.println("logic for son study");
}
}


public class grandson extends son {
public void play() {
         
System.out.println("logic for grand son play");
}
public void study() {
System.out.println("logic for grand son study");
}
}

          public static void main(String[] args) {
         
                   father f1=new father();
                   f1.earn();
                   f1.drive();
                   f1.earn();
                  
          }

}

No comments:

Post a Comment