• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Overriding and overloading

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 74384
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic