Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calling a superclass method

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
thanks all
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by pradeepta chopra:
thanks all

You're welcome
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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