May 1, 2011

Overriding Rules and Tips in Java

Rules for Overriding Methods in Java

Rule 1) A method is said to be overriden if a class which extends another class defines method with the same name and arguments list.

Rule 2) The method defined in the base class should be visible in the derived class. If this is not so, the method in the derived class will not be considered overridden version but will be treated as a normal method.

Rule 3) The method name and arguments list should be same for overriding and overridden methods. But the return type can be co-variant. This means that if the return type of the method in super class is
Map, then the return type of the same method can be HashMap.

Rule 4) The access specifier in the overriding method (in the derived class) should not be more limiting than that of the overriden method (in the base class). This means that if the access specifier for base class method is protected then the access specifier for the derived class method should not be default or private but can be protected, public. The order of increasing visibility of various specifiers is:

private, default, protected, public

Rule 5) The exceptions specified in the derived class method should be either same or sub-class of them. Thus if the base class method specifies the exception as IOException in the throws clause then the derived class method can specify the exception as FileNotFoundException, IOException but not Exception.

Tips for Overriding Methods in Java

Tip 1) Choose to override when the base class/interface version is generic in nature.
Tip 2) Use inheritance and overriding of methods judiciously as it increases coupling between classes.
Tip 3) Try to invoke overridden methods by creating a reference of base class/interface type and making it refer to the derived class object. This helps in coding to generalization and helps creating lesser number of references.

7 comments:

  1. Hi, I´m glad to find your blog, looks really interesting and to the point. I wanted to ask you about the exceptions rule when overriding... when you say either the same or a subclass of the exception, same is exactly the same or the same type?
    I mean if you have the original method throwing a IOException the method in the subclass can throw an SQLException that is at the same level that IOException?

    Or the method in the subclass can only throw IOException and subclasses of it?

    Well, thanks in advance.

    ReplyDelete
  2. No you can't specify SQLException. In fact you can specify any unchecked exception but not any other checked exception except the subclasses of the overridden method. In our example, no other checked exception other than the subclasses of IOException

    ReplyDelete
  3. This is pretty important in terms of SCJP preparation because I have seen many question based on correct implementation of overriding method in Java and differentiating them from overloading.


    Thanks

    Javin

    Difference between ConcurrentHashMap , Hashtable and SynchronizedMap

    ReplyDelete
  4. This post is very clear about override method.

    I confused many times this overriding concept.
    now I am clear about overriding

    Thanks for your post.

    Regards
    ramesh

    ReplyDelete
  5. Overriding is very Big concept in java. And your blog is very usefull and helpfull to everyone..

    Thanks
    Meharaj

    ReplyDelete
  6. HI,
    When i attend the interview i got one confused question.ie
    why do u take the same method name both super and sub classes.whats the advantage,why don't u take different names.

    Can u suggest pleaseeeee?

    Thanks in advance.

    Regards,
    Madhu

    ReplyDelete