aspose file tools*
The moose likes Java in General and the fly likes Overriding and overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overriding and overloading" Watch "Overriding and overloading" New topic
Author

Overriding and overloading

Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
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

Joined: Oct 02, 2003
Posts: 11402
    
  16

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



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
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

Joined: Oct 02, 2003
Posts: 11402
    
  16

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

Joined: Mar 02, 2011
Posts: 77
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

Joined: Oct 13, 2005
Posts: 39393
    
  28
There is one method in the superclass and another different method (with the same signature) in the subclass.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

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

Joined: Sep 18, 2010
Posts: 69

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

Joined: Mar 10, 2008
Posts: 326

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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overriding and overloading