Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Java Inheritance
Java Single Inheritance
Java Super Keyword
Java Multiple Inheritance
Java Hierarchical Inheritance
Java Multilevel Inheritance
Java Method Overriding
Java Abstract Classes,Methods
Java Final Variables, Methods
Java Dynamic Dispatch Methods
Java Visibility Controls

Interfaces

Java Interface
Java Class vs Interface
Java Multiple Inheritance
Java Nested Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Super Keyword


The super keyword is used to invoke the super class constructor from subclass constructor function.

In Java Subclass can access all the members of superclass (base class) except private data members. When the superclass is created which wants to keep it's private data members itself only. In such type of problem Subclass cannot access these members directly.

The solution is whenever a subclass need to refer to it's immediate superclass then use of super keyword is the best solution.


Rules for using super keyword

  • It is only used to invoke the superclass parameterized constructor
  • It must be used from the subclass constructor
  • Super must be the first statement while invoking the constructor

Super is used in two different ways

Super Keyword
  1. For calling superclass constructor
  2. For accessing members of superclass which are hidden by subclass

1.For calling superclass Constructor

We can call superclass constructor as

class classname extends SuperClassName
{
   classname()
   {
      super(); 	
		
      //or
		
      super(parameter_list);
	}
}

super() calls the no argument constructor and super(parameter_list) calls to the parameterized constructor

Program Example

Program to invoke (execute) the Subclass constructor from baseclass Constructor using super keyword

class SuperCons
{
    SuperCons(String msg)
    {
	System.out.println(msg);
    }
}

class MainMethod extends SuperCons
{
    MainMethod()
    {
	super("I'm Parameterized Constructor from class SuperCons");
	System.out.println("I'm Default Constructor from class MainMethod");
    }
    public static void main(String args[])
    {	
	MainMethod m1 = new MainMethod();
    }
}

Output

I'm Parameterized Constructor from class SuperCons
I'm Default Constructor from class MainMethod

In above example we have used super keyword to call Parameterized constructor from baseclass. The default constructor of superclass will not be executed if we call it's parameterized constructor using super


2. For calling Superclass Members

Using super keyword we can call the members of superclass from any function of the baseclass. See below example to understand it

We can call superclass members as

class classname extends SuperClassName
{
   classname()
   {
       super.field_name;	
       //or
       super.method_name();
	}
}

Program Example

class BaseClass
{
    int myValue = 100;
    int BaseValue = 500;
    void show()
    {
	System.out.println("Show from base class, My value = "+myValue);
    }
}

class DerivedClass extends BaseClass
{
    int BaseValue;
	
    void show()
    {
	BaseValue = super.BaseValue;
	super.show();
	System.out.println("Show from Derived class, Base Value = "+BaseValue);
    }
    public static void main(String args[])
    {	
	DerivedClass dc = new DerivedClass();
	dc.show();
    }
}

Output

Show from base class, My value = 100
Show from Derived class, Derived Value = 500

In Above Example, We have created object of class Derived Class and called it's method show(). In show() method we have initialized BaseValue variable from BaseClass using super. And also we have invoked the BaseClass method called as show in same function

<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo