Java Super Keyword
In Java language, the super keyword is used to invoke the base class constructor or method or instance variable.
Syntax:
super;
Example:-
To invoke the base class instance variable using super keyword.
class A
{
int a=10;
void showdata()
{
System.out.println("Base class a value: "+ a);
}
}
class B extends A
{
int a=5;
void showdata()
{ // super invoke the a value from base class
System.out.println("Derived class a value: "+ super.a);
}
}
public class Supermethod
{
public static void main(String args[])
{
B b= new B();
b.showdata();
}
}
Example:-
To invoke the base class method using super keyword.
class A
{
int a=10;
void showdata()
{
System.out.println("Base class a value: "+ a);
}
}
class B extends A
{
int a=5;
void showdata()
{
super.showdata();// super invoke the method from base class
System.out.println("Derived class a value: "+ a);
}
}
public class Supermethod
{
public static void main(String args[])
{
B b= new B();
b.showdata();
}
}
Example:-
To invoke the base class constructor using super keyword.
class A
{
int a=10;
A()
{
System.out.println("Base class a value: "+ a);
}
}
class B extends A
{
int a=5;
B()
{
super();
System.out.println("Derived class a value: "+ a);
}
}
public class Superconstructor
{
public static void main(String args[])
{
B b= new B();
}
}
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com