Bookmark Topic Watch Topic
  • New Topic

calling a superclass method

 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Report post to moderator
We use the keyword super to call the superclass version of an overridden method
for eg,



[B]
[/B]
the above file is saved as Dog.java
it gives a compiler error stating that non static variable super cannot be
referenced from a static context super.dostuff();
^

what does this mean??
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Report post to moderator
The main method is static, and it can only call other static methods and is not allowed to refer to super or even 'this' for that matter, so you'll find you can't even call Dog's doStuff without an instance.
You could call that super.doStuff method from the child's doStuff method.
You could also create some other a non-static method that makes that call, but then you need an instance of the class so that you can call that method.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
You would usually call super.foo() from inside the overridden foo() method, which means you are using the original (superclass = un-overridden) functionality inside the overridden method.
Example in a very commonly overridden methodThat will return whatever the Bar toString method returns, with " Foo" at the end.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
This bit about "non-static . . . cannot be . . . from a static context" is a very common mistake.

You have a static method, which belongs to the class; there is only one copy of that method anywhere in memory.
You have an instance member, which belongs to the object, so there might be several copies of it, with different values in. Which of those are you calling? So the compiler won't allow access. The keywords super and this refer to objects, so they are regarded rather as instance variables, so the compiler won't allow access from a static context either.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
You can only call super when you're overriding methods. As said before in one of your other threads, static methods are not overridden but redefined. As such, super is not allowed.

Since the method is static, you can just call Animal.doStuff inside Dog.doStuff.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Report post to moderator
thanks all
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by pradeepta chopra:
thanks all
You're welcome
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
New questions removed from this thread: please look here, and I think I shall close this discussion.
 
    Bookmark Topic Watch Topic
  • New Topic