Total Pageviews

Friday, 1 July 2011

Method overriding, Dynamic Method Dispatch ,Abstract Class

Method Overriding
When a method in the sub class have the same name and same type of signature as a method in super class, then the method in the sub class is said to overwrite method in super class. When a overridden method is called from within a sub class, it always refer to the method defined in the sub class.The method defined in the super class will be hidden , and we need super keyword to call the method present in the super class.

//example of method overriding
class father{
public int length ,width ;
public  father(int x , int y){
 length = x;
 width = y;
 }
 public void show(){
     System.out.println(" length : " + length + " " + "width : " +width);
 }
}
class son extends father{
    int height;
  son(int x , int y, int z){
  super(x,y);
  height = z;
  }
  public int volume(){
      return height*width*length ;
}
public void show(){
System.out.println("length :" + length + " " + "width :" + width + " " + "height : " + height);
System.out.println("overridden method");
}}
class see{
    public static void main(String args[]){
    son akash = new son(4,5,6);
    int p = akash.volume();
    System.out.println("volume :" + p );
    akash.show();
    }
}


Dynamic Method Dispatch
Dynamic Method Dispatch is an important mechanism in java which helps us to resolve an overridden method at run time, rather than compile time.This mechanism helps us to understand how run time polymorphism occurs.When an overridden method is called by the reference of superclass,Java decides which overridden method is to be executed by looking at the object referred by the superclass reference.


//example of dynamic method dispatch
class student{
   public int roll , marks  ;
   public student(int x, int y){
       roll = x;
       marks = y;
   }
 public void show(){
 System.out.println(" roll no : " + roll + " " + " marks " + marks);
 }}
class studentA extends student{
public int std;
studentA(int x , int y , int z){
  super(x,y);
  std = z;
}
public void show(){
 System.out.println("class " + std + " roll no : " + roll + " " + " marks " + marks);
}
}
class studentB extends student{
public int st;
studentB(int x , int y , int z){
  super(x,y);
  st = z;
}
public void show(){
 System.out.println("class " + st + " roll no : " + roll + " " + " marks " + marks);
}
}
class NewClass{
    public static void main(String args[]){
    student all = new student(12,59);
    studentA A = new studentA(32,95,5);
    studentB B = new studentB(36,96,8);
    student ref; //reference of student class
    ref = all;
    ref.show();
    ref = A ;
    ref.show();
    ref = B;
    ref.show();
}
}

output
roll no : 12 marks 59
class 5 roll no : 32 marks 95
class 8 roll no : 36 marks 95

explanation : Here we have taken a superclass reference "ref" .The superclass's reference is assigned by the objects of superclass and the sub-classes.This reference helps to invoke the show() method. So , the object referred will determine which method is to be executed.

Abstract Class

In the abstract class , we use to declare a generalized form of methods that will be shared by all its sub-classes, leaving it to each sub classes to fill in the details .In other words we can say that , we are just building an empty method in the superclass (abstract class) which will get filled by the methods with same name that are defined in the sub-classes.
syntax :
abstract class <class-name> {
//,,....
abstract void <method-name>();
//;;;
}

note :
# We use the abstract keyword in front of the class keyword to declare the class <class-name> as an   abstract type class.The abstract method declared in the abstract class (superclass) is defined in each sub-classes.
# We cannot create an object of abstract class.
# We can only create the object reference of abstract class because in java, superclass's reference is used to implement run time polymorphism.
# We cannot create abstract constructor and static methods.

//example of abstract class
abstract class student{
   public int roll , marks  ;
   public student(int x, int y){
       roll = x;
       marks = y;
   }
 abstract void show(); //abstract method without body
 }
class studentA extends student{
public int std;
studentA(int x , int y , int z){
  super(x,y);
  std = z;
}
public void show(){
 System.out.println("class " + std + " roll no : " + roll + " " + " marks " + marks);
}
}
class studentB extends student{
public int st;
studentB(int x , int y , int z){
  super(x,y);
  st = z;
}
public void show(){
 System.out.println("class " + st + " roll no : " + roll + " " + " marks " + marks);
}
}
class NewClass{
    public static void main(String args[]){
    studentA A = new studentA(32,95,5);
    studentB B = new studentB(36,96,8);
    student ref; //reference of student class
    ref = A ;
    ref.show();
    ref = B;
    ref.show();
}
}


No comments:

Post a Comment