Sunday, October 12, 2014

What is method overriding?

Method Overriding: It basically means providing a new implementation of a method in sub-class/child class. That means you are defining a behavior of a method that is more specific to subclass/child class.
The following example demonstrate a Horse  subclass of Animal Overriding the Animal version of eat method.

public class Animal {
    public void eat(){
        System.out.println("Generic Animal Eating");
    }
}

public class Horse extends Animal{

     public void eat(){
        System.out.println("Horse eating hey.");
    }
}

For abstract methods you inherit from a super class you have no choice, you must implement the method in subclass unless the subclass is also abstract. Abstract methods must be implemented by concrete subclass. So you could think of abstract method as method you are forced to override.  

For details refer http://java-auth.blogspot.in/2014/10/all-about-abstract-class-and-abstract.html.

The rules of overriding method are as follows:

1. The argument list must exactly match that of the overridden method, if they don't match you end up with an overloaded method which you didn't intend.

2. The return type must be same as or sub type(important) of , the return type declared in the original overridden method in the super class(Check co variant return).

3. The access level can't be more restrictive than the overridden method's.

4. The access level CAN be less restrictive than that of the overridden method.

5. Instance methods can be overridden only if they are inherited by the subclass.
  e.g.  A subclass within the same package as the instance's super class can override any super class method that is not marked private or final. A subclass in a
 different package can override only those non-final methods marked public or protected(since protected methods are inherited by the subclass).

6. The overriding method CAN throw any unchecked (run time) exception, regardless of whether the overridden method declares the exception(for more detail check).

7. The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a    FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-run time exception unless it's a subclass of FileNotFoundException.

8. The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.

9.You cannot override a method marked final.

10. You cannot override a method marked static.

11. If a method can't be inherited, you cannot override it. Remember that overriding implies that you're re implementing a method you inherited! For example, the following code is not legal, and even if you added an eat() method to Horse, it wouldn't be an override of Animal's eat()method as it is private.

public class TestAnimals {
public static void main (String [] args) {
Horse h = new Horse();
h.eat(); // Not legal because Horse didn't inherit eat()
}
}

class Animal {
private void eat() {
System.out.println("Generic Animal Eating Generically");
}
}
class Horse extends Animal { }


Invoking a Super Class version of an Overridden Method:

You can do it using the keyword super as follows:

public class Animal {
    public void eat() { }
        public void printYourself() {
        // Useful printing code goes here
    }
}

class Horse extends Animal {
    public void printYourself() {
        // Take advantage of Animal code, then add some more
        super.printYourself();// Invoke the superclass (Animal) code Then do
        //Horse-specific  print work here
    }
}

Examples of Legal and Illegal Method Overrides:
Let's take a look at overriding the eat()method of Animal:
public class Animal {
    public void eat() { }
}



No comments:

Post a Comment