Programs Using "super" Keyword
Super Keyword
Super Keyword is used to refer the immediate parent class object. Super keyword can be used in three Levels :
- Super at Variable Level
- Super at Constructor Level.
- Super at Method Level
1.super Keyword at Variable Level: When we want to call the variable from the super class then such variables can be called using the super keyword and such level of calling a variable is called as super at variable level.
Programs Using super Keyword at Variable Level
class SuperVariable
{
String instituteName="Void Main Technologies";
}
class SuperVariable1 extends SuperVariable
{
String instituteName="Institute For all The Technical Courses";
void printDetails()
{
System.out.println(super.instituteName); // prints the super class variable Value
System.out.println(instituteName);
}
}
class SuperVariableLevel extends SuperVariable1
{
public static void main(String[] args)
{
System.out.println("Super At Variable Level");
SuperVariableLevel svl=new SuperVariableLevel();
svl.printDetails();
}
}
Save:
SuperVariableLevel.java
Complie:
javac SuperVariableLevel.java
Run:
java SuperVariableLevel
Output:
Institute For all The Technical Courses
Note:
If both super class and sub class Variables are of same type and same name then by using the super keyword we can call the super class Variable.
1.super Keyword at Constructor Level: When we want to call the constructor from the super class then such constructor can be called using the super keyword and such level of calling a constructor is called as super at constructor level.
1.super Keyword at Constructor Level: When we want to call the constructor from the super class then such constructor can be called using the super keyword and such level of calling a constructor is called as super at constructor level.
Programs Using super Keyword at Constructor Level.
class SuperConstructor
{
String instituteName;
SuperConstructor()
{
instituteName="Void Main Technologies";
System.out.println("\t"+instituteName);
}
}
class SuperConstructor1 extends SuperConstructor
{
String caption;
SuperConstructor1()
{
caption="Institute For all Technical Courses";
System.out.println(caption);
}
}
class SuperConstructorTest
{
public static void main(String[] args)
{
SuperConstructor1 sc1=new SuperConstructor1();
}
}
Save:
SuperConstructorTest.java
Compile:
javac SuperConstructorTest.java
Run:
java SuperConstructorTest
Output:
void Main Technologies
{
String instituteName;
SuperConstructor()
{
instituteName="Void Main Technologies";
System.out.println("\t"+instituteName);
}
}
class SuperConstructor1 extends SuperConstructor
{
String caption;
SuperConstructor1()
{
caption="Institute For all Technical Courses";
System.out.println(caption);
}
}
class SuperConstructorTest
{
public static void main(String[] args)
{
SuperConstructor1 sc1=new SuperConstructor1();
}
}
Save:
SuperConstructorTest.java
Compile:
javac SuperConstructorTest.java
Run:
java SuperConstructorTest
Output:
void Main Technologies
Institute For all Technical Courses
1.super Keyword at Method Level: When we want to call the method from the super class then such method can be called using the super keyword and such level of calling a method is called as super at method level.
Programs Using super Keyword at Method Level
class SuperMethod
{
String instituteName;
void superMethod()
{
instituteName="Void Main Technologies";
System.out.println("\t"+instituteName);
}
}
class SuperMethod1 extends SuperMethod
{
String caption;
void superMethod1()
{
super.superMethod();
caption="Institute For all Technical Courses";
System.out.println(caption);
}
}
class SuperMethodTest
{
public static void main(String[] args)
{
SuperMethod1 sm1=new SuperMethod1();
sm1.superMethod1();
}
}
Save:
SuperMethodTest.java
Compile:
javac SuperMethodTest.java
Run:
java SuperMethodTest
Output:
Comments
Post a Comment