• Post Reply 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

What are the rules for using 'super' keyword ?

 
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the rules/restructions/scenarios for how & where we can and cannot use the 'super' keyword.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can
super.<method_Name>() is used to call a method of super class.
super keyword can be use to call the constructor of the superclass in the base class.


can't
super keyword can not be used inside static methods.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 3 use cases of super keyword in java.
For accessing parent class
  • Variables : parent class variable with same name as in child class
  • Methods : parent class method with same name as in child class
  • Constructor: calling parent class constructor


  • For accessing parent class variable and methods you can use super keyword anytime you need.
    However for  calling parent class constructor super should be the first line in the child class constructor.

    You can check Super keyword in java for detailed explanation with examples
     
    Saloon Keeper
    Posts: 26039
    186
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "super" is a disambgiuating reference. The default target of any unqualified field or method reference in class member code is "this", so you use "super" if "this" has an override that you want to suppress. In an inherited class definition, a reference to a field or method defined in the superclass or subclass will be inherited under "this", but also be accessible under "super",

    So "super" is optional if reference via "this" would return the superclass property or method, but mandatory if it would not.

    And, of course, neither "super" nor "this" are valid in static methods.
     
    She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic