Thursday, 20 February 2014

super keyword

Super keyword is used to
1. call super class methods
2. call super class variables
3. call super class constructor

Usage of super keyword are valid only in an instance method, instance initializer, or constructor. If they appear anywhere else, a compile-time error occurs.

1. Call super class methods
      Syntax
      super.methodName(parameters)

Overridden method of the super class is called by using super keyword.
class SuperClass{
 void print(){
  System.out.println("I am super class method");
 }
} 

class SubClass extends SuperClass{

 void print(){
  System.out.println("I am sub class method");
  super.print();
 }

 public static void main(String args[]){
  SuperClass obj1 = new SubClass()
  obj1.print();
 }

}
 
Output
I am sub class method
I am super class method

2. Call super class variables
     Syntax
     super.variableName

class SuperClass{
 String s = "I am super class variable";
 
 void print(){
  System.out.println("I am super class method");
 }
}
   
class SubClass extends SuperClass{
 String s = "I am sub class variable";

 void print(){
  System.out.println(super.s);
  System.out.println(s);
 }

 public static void main(String args[]){
  SuperClass obj1 = new SubClass();
  obj1.print();
 }
}


Output
I am super class variable
I am sub class variable

3. Call super class constructor
     Syntax
     super();
       (OR)
     super(parameter list);

super() is used to call the default constructor of super class.
super(parameter list) is used to call the parametrized constructor of super class.
class SuperClass{
 SuperClass(String s){
  System.out.println("Super Class: " + s);
 }
}
  
class SubClass extends SuperClass{

 SubClass(String s){
  super(s);
  System.out.println("Sub Class: " + s);
 }

 public static void main(String args[]){
  SuperClass obj1 = new SubClass("abcde");
 }

}

Output
Super Class: abcde
Sub Class: abcde

Some Points to Remember
1. Super can't be used in static context, whether it is inside static method or block.
class SuperClass{
 static String s = "Super Class";
}

class SubClass extends SuperClass{
 static String s = "Sub Class";

 public static void main(String args[]){
  System.out.println(super.s);
 }
} 
   
When you tries to compile the above program compiler throws the below error

SubClass.java:5: error: non-static variable super cannot be referenced from a static context
System.out.println(super.s);
^
1 error

2. What is wrong with the below code ?
class SuperClass{
 static String s = "Super Class";

 SuperClass(String s){
 }
}

class SubClass extends SuperClass{
 static String s = "Sub Class";

 SubClass(){
 }

 public static void main(String args[]){
  SubClass s1 = new SubClass();
 }
}

If a constructor does not explicitly invoke a super class constructor, the Java compiler automatically inserts a call to the no-argument constructor of the super class. If the super class does not have a no-argument constructor, compiler provides one default constructor only if the super class doesn't have no constructors like parametrized, Otherwise you will get a compile-time error.

When you tries to run the above program, compiler throws the below error.

SubClass.java:4: error: constructor SuperClass in class SuperClass cannot be applied to given types;

SubClass(){
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
1 error

To make program, run you have 2 options.

Option 1
Provide a default constructor for super class
class SuperClass{
 static String s = "Super Class";

 SuperClass(String s){

 }

 SuperClass(){

 }
} 
  
Option 2
Explicitly call the super class parameterized constructor.
class SubClass extends SuperClass{
 static String s = "Sub Class";

 SubClass(){
  super(s);
 }

 public static void main(String args[]){
  SubClass s1 = new SubClass();
 }
} 

3. Call to super must be first statement in constructor
  

4.Calling super class constructors in any methods other than constructors, cause compiler error.
  


Fields hiding                                                 final keyword                                                 Home

No comments:

Post a Comment