Java: overriding and overloading

Overriding and overloading are common concepts of Java and occur often on SCJP exam. Although I know the rules that apply for them, I happen to think twice (or more) on questions dealing with them. Thus, it’s a good idea to keep the following rules in mind…

Overriding

  • applies ONLY to inherited methods
  • is related to polymorphism
  • object type (NOT reference variable type) determines which overriden method will be used at runtime
  • overriding method MUST have the same argument list (if not, it might be a case of overloading)
  • overriding method MUST have the same return type; the exception is covariant return (used as of Java 5) which returns a type that is a subclass of what is returned by the overriden method
  • overriding method MUST NOT have more restrictive access modifier, but MAY have less restrictive one
  • overriding method MUST NOT throw new or broader checked exceptions, but MAY throw fewer or narrower checked exceptions or any unchecked exceptions
  • abstract methods MUST be overridden
  • final methods CANNOT be overridden
  • static methods CANNOT be overridden
  • constructors CANNOT be overridden

Overloading

  • overloading can take place in the same class or in the subclass
  • overloaded methods MUST have a different argument list
  • overloaded methods MAY change the return type (in case argument list is different)
  • overloaded methods MAY change the access modifier
  • overloaded methods MAY throw new or broader checked excpetions
  • reference type determines which overloaded method will be used at compile time
  • constructors MAY be overloaded
  • methods adjustment in connection with overloaded method’s arguments:
    • you cannot widen and then box (int -> Long)
    • you can box and then widen (int -> Object, via Integer)
    • you can combine var args with either widening (byte -> int) or boxing (int -> Integer):
      • widening is over boxing
      • widening is over var args
      • boxing is over var args

Finally, a few notes on polymorphism:

  • a refenrence variable is of an unchangeable type, but can refer to a subtype object
  • a single object can be referred to by reference variable of many differnet types (however, they MUST be the of same type or supertype of the object)
  • reference type determines which method will be called

Also, keep that in mind:
Reference type determines which overloaded method is used at compile time.
Object type determines which overriden method is used at runtime.

21 Responses to “Java: overriding and overloading”


  • Vert helpful, thank you.

  • Always good to read about boxing.

    Can I ask though – how did you get this picked up and into google news?

    Very impressive, is it something that is just up to Google or you actively created?

    Obviously this is a popular blog with great data so well done on your seo success..

  • I’m very happy that anybody reads all that ;)
    kung-fu-dummies, what do you mean by saying this site was picked up by google news? Did you mean it was high in the result list or something else?

    Cheers,
    Jarek

  • Really very useful..for the students of our college..Thanks

  • Realy very helpful……. thank you

  • Respected Sir,

    Thanks for given detail information about overloading and overriding which i required.

    Thanks & Regards
    Suhas

  • ur writing here “static methods CANNOT be overridden” but in this program it is running successfully :

    class SuperClassWithDifferentMethods2{
    static void method9() { System.out.println(“SuperClassWithDifferentMethods.method9″);
    }
    }

    class OverridingClass2 extends SuperClassWithDifferentMethods2{
    static void method9() {
    System.out.println(“OverridingClass.method9()”);

    }
    }

    public class MethodOverridingDemo2 {
    public static void main(String[] args) {
    OverridingClass oc1 = new OverridingClass();
    oc1.method9();
    new SuperClassWithDifferentMethods2().method9();
    }
    }

  • Subh,
    method9() from OverridingClass2 class shadows method with the same name that is defined in SuperClassWithDifferentMethods2.

    Cheers,
    Jaroslaw

  • so can u tell me wht is shadows method?

  • thanks, very helpfull

  • hi Jarosław,
    can i ask you to explain the proxy and Immutable patterns.
    best regards.

  • Good summary, Very Helpful….

  • Hi Jaroslaw,

    This is reference to the “shadow” comment you made about overriding static methods. Honestly, I don’t understand this, so the compiler wont complain if you override a static method, so why do you think we shouldn’t override static methods. I ran the following code

    public class TestOverriding{
    public static void main(String[] args) {
    A.doSomething();
    B.doSomething();
    A.doSomething();
    }
    }

    class A{
    static void doSomething(){
    System.out.println(“I am A”);
    }
    }

    class B extends A{
    static void doSomething(){
    System.out.println(“I am B”);
    }
    }
    This compiled fine and the following was the output
    ———- java ———-
    I am A
    I am B
    I am A

    Which, I think is what I expected, so I would appreciate it, if you explain me what you meant. Thanks.

  • Such a great helpful content…!!! Thanks a lot Jarosław.

  • Hi, it’s a bit offtopic but may I ask you where did you get this blog template? I’m going to start bloggin as well, I’m a bit noob though but I really like it ;) Let me know…

  • Hal. it’s K2 theme. Can be downloaded here

  • Very helpful. Thanks to all.

  • vary helpful.. thanks a lot..

  • Very Helpful..Thanks

  • thanks a lot Jarosław.. i was not getting the exception thing but your explanation made it easy going..

Leave a Reply