File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about super and sub class static method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about super and sub class static method." Watch "about super and sub class static method." New topic
Author

about super and sub class static method.

Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
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.


Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346
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 (/^_^\)


Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
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

Joined: Jul 18, 2001
Posts: 317
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

Joined: Aug 24, 2001
Posts: 53
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Oct 03, 2001
Posts: 10

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

Joined: May 11, 2001
Posts: 49
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


Asma Zafar,<BR>Sun Certified Programmer for Java2 Platform
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
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

Joined: Jul 18, 2001
Posts: 317
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

Joined: Jul 03, 2001
Posts: 2120
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


SCJP2. Please Indent your code using UBB Code
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: about super and sub class static method.
 
Similar Threads
question from Sun Guoqiao's homepage
overriding of static method
"private" methods are implicitly "final"?
Overriding
Override main method ?