Results 1 to 6 of 6
Thread: Overloading or Overriding...
- 02-21-2011, 03:59 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 27
- Rep Power
- 0
Overloading or Overriding...
Hi, i'm consufed with this...
below code class Tazan has overriden and overloaded methods, (it has both)
Bt when it is called with reference variable, i don't get the expected output..
My idea WAS, overloading reference type and overridding actual object type..
Please help me to verify this Thanks a lot
Output of following prgram is Animal
class Animal{
public void method(Animal A)
{
System.out.println("Animal");
}
}
class Dog extends Animal{
public void method(Dog d)
{
System.out.println("Dog");
}
}
class Tazan extends Dog{
public void method(Animal t)
{
System.out.println("Animal");
}
public void method(Tazan t)
{
System.out.println("Tazan");
}
}
class Test{
public static void main (String args[]){
Animal t=new Tazan();
t.method(t);
}
}
- 02-21-2011, 04:07 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
All that the compiler knows is that 't' is an Animal. It calls the method(Animal ...) method. That method is overridden so the method(Animal ...) method in the Tazan class is called. Note that overloading is the responsibility of the compiler, overriding is runtime business.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-21-2011, 04:10 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 27
- Rep Power
- 0
Hi, is that mean, if we have both overloaded and over-ridden methods (like Tazan class)
overloading will happen! am i correct?
- 02-21-2011, 04:53 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
I don't understand your question but I write it again: overloading is a compiler issue while overriding is done at runtime. If the compiler can't find an appropriate method it will generate an error message. Overriding always works because it has passed all compiler checks.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-21-2011, 05:19 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
overloading depends on different arguments. For examples, you could have a method that adds 2 integers, and then overload it to work on doubles and floats
In your code you doJava Code:public void add(int x, int y){ System.out.println(x + y); } public void add(double x, double y){ System.out.println(x + y); } public void add(float x, float y){ System.out.println(x + y); }
The t is of type animal so it will apply the method which takes an animal, since animal is also overridden it uses the more specific method.Java Code:Animal t = new Tazan();
Also, since you have an overridden method in tazan, try changing the animal method to print out "Tazen, Animal" This way you can tell which version of the method is being called, and this can help you understand what is happening.Last edited by sunde887; 02-21-2011 at 05:22 PM.
- 02-22-2011, 01:46 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Method Overloading - Doubt
By vidya lakshman in forum New To JavaReplies: 4Last Post: 02-01-2011, 06:47 AM -
overloading
By Bhuvan in forum Advanced JavaReplies: 6Last Post: 01-02-2011, 09:15 AM -
Overloading qn
By diskhub in forum New To JavaReplies: 18Last Post: 06-07-2010, 05:19 PM -
Overloading abstract methods??!?
By tfitz666 in forum New To JavaReplies: 2Last Post: 03-07-2010, 09:45 AM -
Overloading?
By padutch2 in forum New To JavaReplies: 2Last Post: 12-31-2007, 03:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks