Wednesday, 19 February 2014

Overriding Methods

A subclass has the same instance method signature like the super class instance method is called overriding.

Example
class Animal{
 void print(){
  System.out.println("I am animal");
 }
}

class Tiger extends Animal{
 void print(){
  System.out.println("I am Tiger");
 }
}

As you observe, the classes Animal and Tiger has the same method signature print(). So print method in the class Tiger overrides the method in the class Animal.

Dynamic Method Dispatch
It is a mechanism, where a call to the overridden method is resolved at run time. It is also called as run time polymorphism.

Take the same examples Animal and Tiger.
class DynamicMethodDispatchEx{
 public static void main(String args[]){
  Animal anim1 = new Tiger();
  anim1.print();
 }
}

Output
I am Tiger

As you see the statement
     Animal anim1 = new Tiger();

An object of the Tiger class is assigned to the Animal reference variable. This is completely correct in Java. We can assign a subclass object to super class reference variable.

When the program calls the print(), then at run time JVM checks, the actual object pointed by the reference variable and calls the respective method of the actual object. So it calls the print method in the Tiger class.

Modifiers
The access specifier for an overriding method can allow more, but not less, access than the overridden method. For example, a default instance method in the superclass can be made public, protected, default, but not private, in the subclass.

Some points to Remember
1. Is run time polymorphism applicable to static methods also ?
No, static methods can't be overridden, they just hide the static method of the super class.

2. Is the instance variables overridden ?
No
class Animal{
 String str = "Animal";
 void print(){
  System.out.println("I am animal");
 }
}

class Tiger extends Animal{
 String str = "Tiger";
 void print(){
  System.out.println("I am Tiger");
 }
}
    
class DynamicMethodDispatchEx{
 public static void main(String args[]){
  Animal anim1 = new Tiger();
  anim1.print();
  System.out.println(anim1.str);
 }
}
   
Output
I am Tiger
Animal

If the instance variables are overridden, then the output should print the message “Tiger”. I.e, instance variables are just hide the instance variables of super class, these can't be overridden.

3. Can I apply static modifier to the subclass overriding method?
No, compiler throws an error.
 
 Example       
class Animal{
 String str = "Animal";
 
 void print(){
 System.out.println("I am animal");
 }
}

class Tiger extends Animal{
 String str = "Tiger";
 
 static void print(){
  System.out.println("I am Tiger");
 }
}
   
When tries to compile the Tiger class, below error thrown.

Tiger.java:3: error: print() in Tiger cannot override print() in Animal
static void print(){
^
overriding method is static
1 error

4. What is wrong with the below program ?    
class Animal{
 String str = "Animal";
 
 void print(){
  System.out.println("I am animal");
 }
}

class Tiger extends Animal{
 String str = "Tiger";
 
 private void print(){
  System.out.println("I am Tiger");
 }
}


When you tries to compile the above program, compiler throws the “Weaker access specifier error”

Tiger.java:3: error: print() in Tiger cannot override print() in Animal
private void print(){
^
attempting to assign weaker access privileges; was package
1 error

The access specifier for an overriding method can allow more, but not less, access than the overridden method. For example, a default instance method in the superclass can be made public, protected, default, but not private, in the subclass.

5. What is the difference between static binding and Dynamic binding ?
Static binding occurs at compile time, where as Dynamic binding at run time. Dynamic method dispatch is an example for Dynamic binding.

6. What is wrong with the below program ?
class Animal{
 String str = "Animal";
 
 void print(){
  System.out.println("I am animal");
 }
}
  
class Tiger extends Animal{
 String str = "Tiger";

 void print()throws Exception{
  System.out.println("I am Tiger");
 }
}

The overriding method cannot throw any exceptions that are not thrown by the overridden method.

When you tries to compile the above program compiler throws the below error.

Tiger.java:3: error: print() in Tiger cannot override print() in Animal
void print()throws Exception{
^
overridden method does not throw Exception
1 error

7. Is the constructors inherited ?
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses.

8. When a subclass object assigned to super class reference variable, then the reference variable can call the methods which are defined in the super class, trying to call the methods which are not defined in super class cause the compile time error.

Example
class Animal{
 void print()throws Exception{
  System.out.println("I am animal");
 }
}

class Tiger extends Animal{
 void print(){
  System.out.println("I am Tiger");
 }

 void show(){
  System.out.println("I am show method");
 }
}

class DynamicMethodDispatchEx{
 public static void main(String args[]){
  Animal anim1 = new Tiger();
  anim1.show();
 }
}
    
When you are trying to compile the program, below error thrown, since the reference variable of class Animal, “anim1” trying to call the method “show: which is not defined in the class “Animal”.

DynamicMethodDispatchEx.java:5: error: cannot find symbol
anim1.show();
^
symbol: method show()
location: variable anim1 of type Animal
1 error

9. Can I specify weaker access specifier to the overriding method in the sub class ?
No


Multi path inheritance                                                 Covariant return type                                                 Home

No comments:

Post a Comment