Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about super and sub class static method.

 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear everyone, I remember this similar question has been asked before, but I failed to find it, so plz take a look.

Thanks in advance.

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guoqiao
wht exactly is ur q about dis code ?
as per my knowlegde , its behaving as expected
class method can be re-declared in the sub-class , the new definition hides the original one.
------------------
Gagan (/^_^\)
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In this link Sandeep's explanation helps to understand why
and when we get ambiguous error.
http://www.javaranch.com/ubb/Forum24/HTML/011170.html
Vanitha.
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Vanitha, that is what I want to find.
Gagan, sorry for not uttering clearly. My question why
<code>
method(3); // 1
</code>
does not compile while
<code>
method(3L); //2
</code>
compile correctly. Since at first glance, //1 and //2 both should compile.
Regards,

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
Jennifer Warren
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i tried the code with both the calls to the method i.e. with integer and long, there was no error and the out was three printed out twice once the Super method was executed mext the T058 method.
Thanx.
Jennifer.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code originally posted works fine for me. I am using JDK 1.3 in Windows 2000.
Here's the code ...




And, here is the output...



Shyam

[This message has been edited by Shyamsundar Gururaj (edited September 11, 2001).]
 
Peter Java
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is this the one you are looking for?
class Super {
public void method(int i)
{
System.out.println("Method From Super: " + i);
}
}
class aaa extends Super
{
public static void main(String[] args)
{
new aaa().method(3);
new aaa().method(3L);
}
public void method(long i)
{
System.out.println("Method From aaa: " + i);
}
}
 
Asma Zafar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code compiles and runs just fine even if the commented line is uncommented.
Its output is as expected. Static methods are called according to the reference type. If no qaulifier (class or object name) is used, its called from the class where its being invoked, if the same signature method is not in the invoking class it goes up the hierarchy to find one, which is the case in your quoted example.
------------------
Asma Zafar,
Sun Certified Programmer for Java2 Platform
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
The code originally posted by Sun gives error in Java 1.2.
I am in hurry I will come and write about.

Reference to method is ambiguous. It is defined in void method(long) and void method(int).
method(3);
^
1 error

Thanks,
Vanitha.
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all, I am using Forte 2.0 with JDK 1.3.
If I uncomment method(3), the error message from Forte is:

But when I use JDK 1.3 to compile it, it compiles fine even if I uncomment method(3).
Really confused.

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody
Section 15.12 in JLS deals with this subject.
The point is that the example doesn't produce a ambigous compile error because the methods are static and they are placed in the super and subclass. With instance methods the error exits:

uncommenting the lines produce the ambigous error in JDK 1.3
I haven't been able to find an explation for this difference in the behaviour in the JLS
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic