Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Why its calling super class method?

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI All,
Can some one explain why its calling super class add(int a) method?

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bala Rajamani:
[B]HI All,
Can some one explain why its calling super class add(int a) method?

[/B]



Good Question Bala

In your code, there is no overriding, just overloading and that too happens in the subclass.
so when you invoke add method polymorphically
(ie d t = new test2())
it cant invoke your subclass add method , even though your
input parameter may match the subclass's add method's signature
HIH
Ragu
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is a good question - stumped me.
However, I still fail to understand the explanation
given - I thought that when a method is invoked its
the class of the object denoted by the reference
and not the reference itself comes into play. So
that said I would think the add(char c) in test2
class could have been executed.
Regards,
PJ
really been executed
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prashanth Joisha:
Hi,
This is a good question - stumped me.
However, I still fail to understand the explanation
given - I thought that when a method is invoked its
the class of the object denoted by the reference
and not the reference itself comes into play. So
that said I would think the add(char c) in test2
class could have been executed.
Regards,
PJ
really been executed


Yeah Prashanth i understand whats your doubt is
Its sometimes confusing
But here is a thought:
What you said about polymorphic/runtime lookup is perfectly correct Provided if there is overriding!!!
But here there is no overriding.. As a matter of fact, add methods in super and sub classes are different significantly (overloading) and Polymorphism wont cut it
To confirm this please do the following
test2 t2 = new test2();
t2.add(1);
You will still get output from superclass, becoz add() from super is inherited but not yet overrided , just overloaded thats all
Please let me know if you need more clarification
Ragu

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps this may shed some light:
First, suppose that the visibility of the add() method in superclass d is changed from default to private. The compiler will complain:
<pre>
C:\java>javac test2.java
test2.java:22: No method matching add(char) found in class d.
t.add('a');
^
1 error
</pre>
Hmm. Okay, back to the original code:
Apparently, there's some polymorphism and dynamic method lookup issues involved. The class test2 will inherit the superclass method add(int), thus having two methods by the name of add() with different argument types. In the code <pre>d t = new test2();</pre>, a superclass reference of type d is refering to an object of subclass type test2. According to Mughal and Rasmussen, if a subclass does not override an inherited method, the inherited version of the method will be executed. The add(char) method in subclass test2 does not override the add(int) method in superclass d (the arguments are different).
So, I think dynamic method lookup doesn't know that test2.add(char) exists when invoking add() via the superclass reference type. So, it can only rely on d.add(int) and automatically promotes the char value to an int. I think this point is reinforced by the above experiment in declaring d.add(int) private.
Now, suppose we change the argument type of d.add(int) to char. The result:
<pre>
C:\java>java test2
char in sub
</pre>
I hope this help.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is similar to a question I posted earlier.
Take a look at it. http://www.javaranch.com/ubb/Forum24/HTML/012362.html
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overloading across classes can get very interesting and confusing. We've had a discussion abt. this a few days ago, have a look at this. Here's a simple tip abt how to remember this -
Whenever a method of a class is invoked such as : someclass.aMethod(), a compiler will *ALWAYS* look for the declared type of someclass variable (the declared type of the variable that is on the left hand side of the dot of the method), and not at the actual object created at runtime.
If the actual object created overrides this mehod, then that implementation of this method (aMethod) will be invoked at runtime, not otherwise.
This code will illustrate this -
<pre>
class B
{
public void foo(int i)
{
System.out.println("Inside Base foo(int)");
}
}
public class D extends B
{
public void foo(long l)
{
System.out.println("Inside Derived foo(long)");
}
public static void main(String args[])
{
long ln = 123;
B obj = new D();
obj.foo(ln); //compiler error
}
}
</pre>
Here compiler gives compilation error because method foo(long) does not belong to class B.
Remember that, even in terms of overriding, the compiler still OKs the code because that method exists in the base class. It is only at runtime, the objet is created using new and its corresponding implementation (the overriden method) is called.
HTH,
- Manish
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS actually explains it and it has been discussed here before. http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#20448
The way it works is,
1) Q: does the declared type of the object (d) have a method that supports the request?
A: Yes, because d has a method called add() that can take an int as a parameter and an int can be used in place of a char
2) Q: Does the actual type (test2) override that method?
A: No, test2 has no method that overrides add(int)
Result: run d.add(int)
 
pjoisha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
After digesting the responses, I agree that test2 inherits the
add(int a) method and so at runtime object test2 has 2 overloaded
methods - ie add(char c) and add(int c). Hereafter, I differ
as one response put it - "Dynamic Method lookup doesn't know
that test2.add(char c) exists when invoking add() via the superclass reference type". IMHO I think the Dynamic Method lookup does know about the 2 methods. However, I think the compiler is promoting the '1' char to int 49 (ASCII equiv.) and therefore the runtime quitely calls the add(int i) inherited by test2.
I'm begining to see a lot of debate on this subject has happened
before but I'm yet to get a clear picture.
Thx,
PJ
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prashanth,
I agree with you. But, I like the way Thomas summarized how
this kind of question can be dealt correctly.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prashanth
Dynamic method look up doen't recognize add(char) overriding add(int) because they have different signature.
Thomas has sumarized it, and I think all the previous links show what is happenning (Especially the JLS).
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic