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 Overridden methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overridden methods" Watch "Overridden methods" New topic
Author

Overridden methods

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Only Instance methods can be overriden and calls to super only apply overriden methods.

for eg:

class a
{
int getRating{ return 43;}
static int getRating2{ return 42;}
}
class b extends a
{
public static void main(string s[])
{
b.go();
System.out.println(super.getRating2() ;
}
void go()
{
System.out.println(super.getRating());
}
}
}

Output:
Compliation fails
Can anyone expalin please.



Thanks<br />Dinesh
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What was the error message that you saw when you tried to compile your code?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
more than compilation it seems to have lots of Syntax error.
Can you please post real code?

V


SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
cannot used super in static method.


class a
{
int getRating()
{
return 43;
}
static int getRating2()
{
return 42;
}
}
class b extends a
{
public static void main(String s[])
{
b b1 = new b();
b1.go();
System.out.println(super.getRating2() ) ;
}
void go()
{
System.out.println(super.getRating());
}

}
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
super is not allowed in static method.
main() is static method hence U cant use super variable here

V
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
How to covercome this problem
Ivan Ivanic
Ranch Hand

Joined: Oct 31, 2007
Posts: 100
well you don't have a problem. you can access static method getRating2 directly from main, since it is static and inherited from class a. just kick out the super keyword.


<a href="http://faq.javaranch.com/java/UseRealWords" target="_blank" rel="nofollow">Use Real Words</a> <a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">!!!Use Code Tags!!!</a> <a href="http://faq.javaranch.com/java/SayThanks" target="_blank" rel="nofollow">Say Thanks</a><br />scjp6
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overridden methods