Showing posts with label Overriding. Show all posts
Showing posts with label Overriding. Show all posts

Wednesday, 4 November 2015

Rules of method overriding in java : The Pictorial Way

In the previous post we have seen what is method overriding and how we can access overridden methods. In this post we will cover what are the rules to override a particular method from superclass to subclass.

#Rule 1 : Argument list must exactly match


#Rule 2 : Return type should be same as of supper class method or it can be a covariant return type.

What is covariant return type?
Subtype of original return type is known as covariant return type

#Rule 3 : Access level cannot be more restrictive


Why derived class overriding method should not be more restrictive than base class method in java?
Suppose someone access Animal class and thinks eat() is a public method and so it is safe to use this method with Animal class reference, and if at runtime JVM binds Animal reference with Horse object and Horse object have eat() as private method about which the person who is accessing Animal class eat() is completely unaware, than program will crash so derived class overriding method should not be more restrictive than base class.


#Rule 4 : Overriding method cannot throw new or broader checked exceptions


Why derived class overriding method should not throw broader exception than base class?
The reason behind this is same as above explanation, if derived class throws broader exception and JVM binds the base class reference with derived class object, than the code, which is accessing base class method, will fail. 


#Rule 5 : You cannot override final and static method
Why static method cannot be overridden in subclass in java ?
This is because at run time while accessing the static method compiler will replace the instance with class name, and so it will always access the method to which reference is pointing and not to which object is pointing.
However though you cannot override a static method in subclass, you can always redline static method in subclass.







Sunday, 1 November 2015

What is method overriding in java with example : The Pictorial Way

Overriding Method 

I have explained how to override a method in below image.

As I said, I like to explain things in pictorial way, which is compact and easy way to understand, so you will find very less text in my posts, but if at some point you don't understand any concept you can always ask question and I will be happy to help you.

In below diagram you will get answer to following questions.

  • What is Overriding in java with example? 
  • How to access overridden methods in java?

What is overridden method in java ? 
A method in subclass is called overridden method if it satisfies below conditions
  • Same method name as of in Super class
  • Same or broader access specifier
  • Same return type or covariant return type (I will explain covariant return type in next post)
  • Exactly same argument list.
Accessing overridden method in java :
As you can observe in image, no matter what is the reference type, Java will always consider the object to which the reference is pointing.

Why object type and not reference type determines which overridden method to invoke ?
This is because overriding method is a type of run time polymorphism, by this I mean the method binding is done at run time depending upon the object being referred  at that point of time.


How to invoke super class version of overridden method?
To invoke super class version you can use "super" keyword.
Suppose in our above example if you want to invoke first eat method of Animal class and than you want to continue with eat method of horse class; in this scenario you can change eat method of Horse class in following way.

public void eat()
{
super.eat();
//followed by Horse specific functionality

}

When to use overriding method?
Overriding is useful in following scenarios
  • If you want to add some functionality to the method existing in super class.
  • If you want to suppress the functionality of the method present in super class.


In the next post I will explain all the rules to override a method in java.