Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overridden methods

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was the error message that you saw when you tried to compile your code?
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
more than compilation it seems to have lots of Syntax error.
Can you please post real code?

V
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
super is not allowed in static method.
main() is static method hence U cant use super variable here

V
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to covercome this problem
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic