CodingBison

There are two use-cases of the keyword "super". First, "super" keyword is used to call the super class constructor and second, it is used to access super-class members. The below code snippet ( simplified version of the earlier inheritance example ) helps in understanding both the use-cases of the key word "super".

 class movie {
     String name;
     int    length;
     String director;
     int    temp;
     /**
      * Default constructor.
      */
     movie() {
         System.out.println(" Super Class Default Constructor");
         this.name = "";
         this.length = 0;
         this.director = "";	
     }
     /**
      * @param name
      * @param length
      * @param director
      */
     movie(String name, int length, String director) {
         System.out.println(" Super Class Parameterized Constructor: ");
         this.name = name;
         this.length = length;
         this.director = director;
         this.temp = 100;
     }

     int getTemp() {
         System.out.print(" Super class temp: ");
         return temp;
     }
 }

 class jamesBondMovies extends movie {
     String bondActor;
     String bondGirl;
     String mRoleCharacterName;
     int    temp;
     /**
      * Default constructor.
      */
     jamesBondMovies() {
         super();
         System.out.println(" \t Subclass Default constructor: ");
         this.bondActor = "";
         this.bondGirl = "";
         this.mRoleCharacterName = "";
     }

     jamesBondMovies(String name, int length, String director, 
                             String bondActor, String bondGirl, 
                             String mRoleCharacterName) {
         super(name, length, director);
         System.out.println(" \t Subclass Parameterized constructor: ");
         this.bondActor = bondActor;
         this.bondGirl = bondGirl;
         this.mRoleCharacterName = mRoleCharacterName;
         this.temp = 200;
     }

     int getTemp() {
         System.out.print(" Sub class temp: ");
         return temp;
     }

     int getSuperTemp() {
         System.out.print(" From Sub class temp, getting super class temp Value: ");
         return super.temp;
     }
 }

 class InheritanceExample {
      public static void main(String[] args) {
         movie           movieObj1 = new movie();
         jamesBondMovies jbondObj1 = new jamesBondMovies();
         jamesBondMovies jbondObj2 = new jamesBondMovies("Casino Royale", 144, 
                     "Martin Campbell", "Daniel Craig", "Eva Green", "Judi Dench");

         // A sub class Object can be assigned to a Super class Object.
         movieObj1 = jbondObj2;

         System.out.print(" P1: ");
         System.out.println(jbondObj2.getTemp());
         System.out.print(" P2: ");
         System.out.println(jbondObj2.getSuperTemp());
         System.out.print(" P3: ");
         System.out.println(" Accessing Super class member directly: " + movieObj1.temp );
         System.out.println(" P4:  Error ");
         //System.out.println(" Accessing Sub class member " + movieObj1.bondActor);
         System.out.print(" P5: ");
         System.out.println(" Accessing Sub class member: " + jbondObj2.bondActor);
     }
 }

First Usage of keyword "Super"

Constructors are always called in the hierarchical order, notice the below output (both in output 1 and 2), at the time of object creation, the super-class constructors are always called first. If you notice output 1 in detail, even when the "super()" statement is commented out the constructors are called. Then, here is the question we need to answer, what is the need of "super" when the constructors are called anyway ?

Look at the output 1 more closely, Its the super-classes "Default" constructor that's called all the time, which is not the desired action in the case of object "jbondObj2". When the super statements are un-commented out, we achieved the desired actions. Notice the below output (output 2) at the time of "jbondObj2" object create.

Second Usage of keyword "Super"

The other usage of keyword "super" is that, it is used to access the super-class members. In the above example variable "temp" is defined in both sub-class as well as super-class and sub-class "temp" always overrides the super-class temp. In order to access the super-class "temp", "super" key word is used. (The member variable temp in the above example may not make sense, it is merely used to demonstrate the usage of "super")! Notice the statement P2 from the below output, "super" keyword is used to retrieve the output of super-classes "temp".

Assign sub-class object to a super-class object

It is possible to assign an object of sub-class to a super-class object. Inheritance allows this as it is quite useful in many scenarios. In the above example, notice the statement "movieObj1 = jbondObj2;" which does exactly this. From the output(P3, P4, P5) below, we should note the way the member variables are accessed using super-class object. Take a look at the statement P4, it is commented out as it throws a compilation error. Inheritance does not allow this, it is not possible to access sub-class members using a super-class object. It completely makes sense as, super-class is not aware of the contents of a sub-class.

Compile/Run this program and here is the output:


 Output 1: When "Super" statements are commented out.
  Super Class Default Constructor
  Super Class Default Constructor
  	 Subclass Default constructor: 
  Super Class Default Constructor
  	 Subclass Parameterized constructor: 
  P1:  Sub class temp: 200
  P2:  From Sub class temp, getting super class temp Value: 0
  P3:  Accessing Super class member directly: 0
  P4:  Error 
  P5:  Accessing Sub class member: Daniel Craig


 Output 2: When "Super" statements are un-commented.
  Super Class Default Constructor
  Super Class Default Constructor
  	 Subclass Default constructor: 
  Super Class Parameterized Constructor: 
  	 Subclass Parameterized constructor: 
  P1:  Sub class temp: 200
  P2:  From Sub class temp, getting super class temp Value: 100
  P3:  Accessing Super class member directly: 100
  P4:  Error 
  P5:  Accessing Sub class member: Daniel Craig





comments powered by Disqus