Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • super and this Keyword

    • 0
    • 2
    • 0
    • 1
    • 1
    • 0
    • 0
    • 0
    • 69
    Comment on it

    Super Keyword

    The super keyword in java is the one which is used to refer to the immediate base class members in the sub class.

    Super Keyword can be used to invoke the constructor of the base class.

    Whenever you create the object of the subclass, an instance of parent class is created automatically. which can be referred by the super keyword. It allows to access only the non-private members of the super-class.

    syntax:

    super (parameter list); //parameter list for the constructor of the super class.
    

    In the above code the super keyword is used to call the constructor of the super class. To access the methods of the base class using super keywords

    Syntax is as :

    super.methodname(parameter list); //methodname(parameter list) is the base class        // non private member function. 
    

    this Keyword

    The this keyword allows to refer current class methods/attributes with its private members. it is used to access the methods/attributes of the object. It invokes the current class methods and the constructor

    The syntax to access the current current class constructor is as:

    this()  // Invokes current class constructor
    

    To access the other members like function or the variables of the same class we can use this keyword as :

    this.methodname(parameter list); //methodname(parameter list) is the current class method
    

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: