SUPER KEYWORD IN JAVA
Super keyword in java mainly used in three concepts
1.variable.
2.method
3. constructor
1.variable: if we use super keyword with variable then it can be access parent class variables in sub class.
syntax: super.variablename;
Example:
class Flag
{
String name="jay";
}
class Test1 extends Flag
{
String name="viru";
void show()
{
System.out.println(name);
System.out.println(super.name);
}
}
class Sam
{
public static void main(String ar[])
{
Test1 ob=new Test1();
ob.show();
}
}
output:
viru
jay
2.method: when we use super keyword with method than we can access parent class method in sub class.
Syntax : super. Method name;
Example :
Class sum
{
Void show ()
{
System. Out. Println("good morning ");
}
}
Class sample extends sum
{
Void show()
{
System. Out. Println("hello Java learning point ");
}
Void xyz()
{
Show();
Super. Show();
}
}
Class test
{
Public static void main(String args[])
{
Sample ob= new Sample();
On. Xyz();
}
}
Output:
Good morning
Hello Java learning point
3. Constructor : we can also be use super keyword with parent class constructor.
Example
Class demo
{
Demo()
{
System. Out. Printl("constructor is created ");
}
}
Class flag extends demo
{
Flag()
{
super();
System.out.println("hello");
}
}
Class test
{
Public static void main(string args[])
{
Flag ob= new ob();
}
}
Output
Constructor is created
Hello