91-9990449935 0120-4256464 |
super keyword in javaThe super keyword in java is a reference variable that is used to refer immediate parent class object. Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable. Usage of java super Keyword
1) super is used to refer immediate parent class instance variable.Problem without super keywordOutput:100
Solution by super keyword Output:50 2) super is used to invoke parent class constructor.
Output:Vehicle is created Bike is created Note: super() is added in each class constructor automatically by compiler.As we know well that default constructor is provided by compiler automatically but it also adds super() for the first statement.If you are creating your own constructor and you don't have either this() or super() as the first statement, compiler will provide super() as the first statement of the constructor. Another example of super keyword where super() is provided by the compiler implicitly.Test it NowOutput:Vehicle is created 10 3) super can be used to invoke parent class method
Output:welcome to java welcome
Program in case super is not requiredTest it NowOutput:welcome
Next TopicInstance Initializer Block
|