Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding and overloading

 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys i have a question regarding overloading and overriding.

When you override a method, say



Do you create a different method or modify the existing method? Do i have only 1 doStuff method here or 2?


And when you overload a method, do you modify the existing method or do you create a new method?

Thanks in advance
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you overload, you create a second (or third, or fourth...) method with the same name, but with different argument types. So you could overload the B class's doStuff() to be something like

 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:When you overload, you create a second (or third, or fourth...) method with the same name, but with different argument types. So you could overload the B class's doStuff() to be something like



and when you override?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you override, you create a method that is only seen by the sub-class. So if you have an A object and call the doStuff() method, you'll get the version defined in the A class. When you have a B class, you'll get the B-class version.

Note that due to something called "polymorphism", it doesn't matter what the REFERENCE type is. If I do this:


The reference is an A, but the actual object is a B. So I will get the B version of doStuff().

 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:When you override, you create a method that is only seen by the sub-class. So if you have an A object and call the doStuff() method, you'll get the version defined in the A class. When you have a B class, you'll get the B-class version.

Note that due to something called "polymorphism", it doesn't matter what the REFERENCE type is. If I do this:


The reference is an A, but the actual object is a B. So I will get the B version of doStuff().



Thanks, so what you're saying is that there will be one method, not 2 different methods?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49430
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is one method in the superclass and another different method (with the same signature) in the subclass.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both methods exist inside the JVM. However, I don't believe you can say 'this time I want to run the superclass version...this time I want to run the sub-class version". The rules (as I understand them) say that the most specific allowable version will be run. if the actual object is an A, you can't get the B-version to run. If the actual object is a B, you can't get the A-version to run.

 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remenber one thing only (THE GLOBAL RULE(I call))

method overidding -> depends on object type
method overloading -> depends on reference type.


there is nothing polymorphism , when you understand thid RULE.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you overload a method, you have the "same method" react on different input like the print-method in a class:

public void print(boolean b)
public void print(char c)
public void print(char[] s)
public void print(float f)
public void print(double d)
public void print(int i)
public void print(long l)
public void print(Object obj)
public void print(String s)


When you override a method you are redefining a method from a super class. Like the toString()-methods.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic