The Best Technical Blog Ever

twitterrss

Java- super keyword

Declaring the same method in subclass as declared in base class (parent class) is known as method overriding. Whenever the base class version of the function is called which is overridden in the derived class, the super keyword can be used. So whenever a subclass needs to refer to its immediate base class, it can do so by the use of super. Since super keyword represents an instance of the direct parent(immediate base class). It has two general forms. They are,


  1. The first is used to access a member of the base class that has been hidden by a member of a subclass.

  2. The second calls the base class constructor.

Before learning the concepts of super keyword, we will first look at an example that will highlight the problem.


Example :
super


Here, the display() method inside the display() of class Y will call itself rather than calling base class version of display() method and will lead to recursion.


1. Accessing a member of the base class using super:

Using super keyword, you can access any member of the base class that is visible from the subclass. The super.memberName returns the specified member(either a method or an instance variable) of the parent class.


To avoid unwanted recursion problem from the above program, simply use super.display(). It will call display() of class X.


This super keyword format is applicable to situations in which member names of a subclass hide members by the same name in the super class. Since, you can declare a field in the subclass with the same name as the one in the super class thus, hiding it. This is called shadowing. See the example given below.


Example :
super


Even though the instance variable s in Member class hides the s in Zoo class, the super keyword allows to access s defined in the base class. Here, the expression super.s represents the member of the immediate base class Zoo. Similarly super.display() represents display() method of base class Zoo.


Invoking a parent's member (super.Member) does not have to be the first statement in the caller method.


2. Calling the base class Constructors using super:

Constructors use super() keyword to invoke the super class's constructor. Every constructor calls its super class constructor implicitly or explicitly. If a class does not include an explicit super() call as its first statement, then the implicit super() method is invoked by the compiler to call super class's default constructors. If a sub class includes explicit super(), then it replaces the call of implicit super(). The super statement must be the first statement of the constructor; otherwise, the compiler will report error message. A subclass can call a base class constructor using,


super(arglist);

Here, arglist specifies the arguments needed by the constructor in the base class.


The explicit super() invokes the constructor with the equivalent parameter list of the super class. Hence, the explicit super() allows to pass parameter values to the super class constructor that matches its parameter types. This phenomena of calling one constructor from another constructor is known as constructor chaining.


If a super class only defines parameterized constructors , then its subclasses will not include an implicit super() call; Otherwise it will be flagged as a compile-time error. Because, implicit super() invokes only default constructor of super class. So, subclasses must then explicitly call a super class constructor, using the super() construct with the right arguments to match the appropriate constructor of the super class. Below is an example of a class demonstrating the explicit super()


Example :
constructor


constructor


If a super class has a parameterized constructor, then it is the duty of the child class constructor to call the super class's constructor first before executing any other Java statements inside its constructor, otherwise it will create a compile time error.


Keep in mind that any other member functions apart from constructor cannot call super class's constructor. It is obvious that super class's object will be constructed first before child class's object construction.


Example :
inheritance


In the above program, when statement ColourSub s = new ColourSub("red", "pink"); executes, object s calls the parameterized constructor of ColourSub class and passes parameters red and pink. But, before entering into the body of constructor, here super(s) calls the constructor of ColourBase class and passes s as argument. When control returns, it enters into the body of the constructor.


Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status