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();
	}   
}



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.