Monday, 13 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;

publicclass father {

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


publicclassgrandsonextends son {
publicvoid play() {
         
System.out.println("logic for grand son play");
}
publicvoid study() {
System.out.println("logic for grand son study");
}
}

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

}

No comments:

Post a Comment