Sparkles

that were shone when I got tempered!

Tips about Overloding n Overloading

leave a comment »

Overriding Rules

  • Argument list : Must exactly match that of the overridden method. – else overload
  • Return type : Must be the same as, or a subtype of, the return type declared in the overridden method
  • Access level : Can’t be more restrictive (CAN be less restrictive) than the overridden method’s.
  • Exceptions
  • Unchecked Exceptions : CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception
  • Checked Exceptions : Must NOT throw checked exceptions that are new or broader (wider) than those declared by the overridden method
  • Note that final methods cannot be overriden and you MUST override the abstract method (if the class is not an abstract)

Overloading Rules :

  • Overloaded methods MUST change the argument list.
  • Overloaded methods CAN change the return type.
  • Overloaded methods CAN change the access modifier.
  • Overloaded methods CAN declare new or broader checked exceptions.

Invoking (Overriding / Overloading)

Even though the actual object at runtime is a Horse and not an Animal, the choice of which overloaded method to call is NOT dynamically decided at runtime.
The reference type (not the object type) determines which overloaded method is invoked!

Which overridden version of the method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but
which overloaded version of the method to call is based on the reference type of the argument passed at compile time.
If you invoke a method passing it an Animal reference to a Horse object, the compiler knows only about the Animal, so it chooses the overloaded version of the method that takes an Animal. It does not
matter that at runtime there’s actually a Horse being passed.

eg :

class Animal { }

class Horse extends Animal { }

class UseAnimals {

 public void doStuff(Animal a) {
 System.out.println("In the Animal version");
 }

 public void doStuff(Horse h) {
 System.out.println("In the Horse version");
 }

 public static void main (String [] args) {

 UseAnimals ua = new UseAnimals();

 Animal animalObj = new Animal();
 Horse horseObj = new Horse();
 Animal animalRefToHorse = new Horse();

 ua.doStuff(animalObj);
 ua.doStuff(horseObj);
 ua.doStuff(animalRefToHorse);

 }

}

Summery

Screenshot from 2014-07-13 16:52:17

Advertisements

Written by Namal Fernando

July 13, 2014 at 4:50 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.

Ziplok

Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language

Sparkles

that were shone when I got tempered!

%d bloggers like this: