Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Inheritance and method overloading

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody I have a doubt illustrated by this example:
<code>
class Point{
void aMethod(int f){
int var = f;
System.out.println("This is the superclass"+var);
}

}
public class Subpoint extends Point{
public static void main(String[] args){
Subpoint s = new Subpoint();
s.aMethod(2);
}

void aMethod(float f){
float var = f;
System.out.println("this is subclass"+var);
}
}
</code>
The program prints "this is a subclass2.0". That is OK but my doubt is does the subclass Subpoint also not inherit the method aMethod(int f) from the superclass Point and thus we have in reality two overloaded methods of aMethod in the subclass Subpoint? In that case why the subclass method aMethod(float f) is invoked and not aMethod(int f) ?
I know methods can be overloaded only within that class, then what happens to inheritance?
If I am not clear please tell me.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howard,
No. The given program will not compile at all. How come you got the result as you said?. This program is an excellant example of the more specific concept. The compiler always picks up the more specific method from available methods if an arg to a method is compatible to more than 1 method. But the rule is this.
Assuming there are 2 methods void print(int i) {} , and void print(float f) {} and we invoke this method as print(10). Let us analyze this case. Assuming there is a base class and a subclass which extends this base class. We also assume that we create a subclass object and invoke this method on this subclass object. So the foll. are the possible schenorios.
1. Both methods can be defined in base class
2. Both methods can be defined in sub class
3. The more specific method can be defined/overridden in sub class
4. The more specific can be in base class.
So the compiler follws these rules.
The compiler can easily pick up the more specific method in the first 3 cases. In the last case , it is confused. Since the more specific method is in base class and inherited to this subclass it does not know which one to pickup. So it throws the compile time error saying it is ambiguious. Your program is an example of the 4th case.
The same rules apply for methods taking objects as arguments also. If I am not wrong, this topic already came up before and Jim discussed very good. I try to find out this link.
Regarding your last comment saying methods can be overloaded only within the class is not correct. A method can be overloded either in the same class or in the subclass or both.
regds
maha anna

[This message has been edited by maha anna (edited March 16, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maha.
I did go back to the program and found that the compiled program was exactly same as the program mentioned in my query except for the fact that I had used a different class name "Test" in the compiled program instead of "Point" in the subclass declaration "extends ...". This was indeed a mistake while coding that I hadn't noticed. I have recompiled and run the program and found the error being generated because of the ambiguity of the two methods.
As far as the overloading is concerned I was mislead by a statement in Jamie Jaworski's book page 149, "Method overloading consists of using the same method name with different arguments and return types within the same class".
Thanks for taking the pains to clarify these issues.
I might recommend you in the "Sun of the beach!!!"(Just kiddin')
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just one more thing the class "Test" that was there by mistake was one of the classes in the package so the program compiled.
What happens if the variable s is declared as type Point instead of Subpoint
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then it will be fine. Because the more specific method is in the same class as of the object's class on which the method is invoked. No compile error will occur.

regds
maha anna

[This message has been edited by maha anna (edited March 16, 2000).]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[/B]
</BLOCKQUOTE>
Ex. 3 will also give ClasscastException at runtime!
I think you meant:

[This message has been edited by Jane Rozen (edited March 17, 2000).]
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jane,
I don't get you. What I said for Ex.1 / Ex.2 / Ex.3 are all correct.
Spcifically Ex.3 wouldn't even pass the compilation. Then what's the point in talking about run time? May be I am missing some of your point. Can you explain what you said in previous post?
regds
maha anna
 
Jane Rozen
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think you meant:

</BLOCKQUOTE>
No offence, Maha, I just thought it's better to limit your ex.3 to just ONE error - the one you were discussing.
(sorry, just teacher's habit)

[This message has been edited by Jane Rozen (edited March 17, 2000).]
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I think I should limit myself when I give examples.
Thank you.
regds
maha anna
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't visit the site for sometime. The question was indeed
<code>Point s=new Subpoint();</code> as Jane pointed out.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When Point pt = new SubPoint();
pt.aMethod(10); is called, no problem. Since you are creating a Subpoint object and auto converting to Point , and now the class Point has only one aMethod(int i), which is compatible with aMethod(10), the compiler is happy. At run time, 'This is superclass10' will be printed.
I hope this clears your doubt completely.
regds
maha anna
[This message has been edited by maha anna (edited March 17, 2000).]
 
You have to be odd to be #1 - Seuss. An odd little ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic