super() java code example

Example 1: super keyword in java

Variables and methods of super class can
be overridden in subclass. 
In case of overriding , a subclass
object call its own variables and methods.
Subclass cannot  access the variables and
methods of superclass because the overridden
variables or methods hides the 
methods and variables of super class.
But still java provides a way to access 
super class members even if 
its members are overridden. Super is
used to access superclass variables, methods, constructors.
Super can be used in two forms :
1) First form is for calling super class constructor.
2) Second one is to call super class variables,methods.
Super if present must be the first statement.

Example 2: what is super keyword

Variables and methods of super class can be overridden in subclass. 
In case of overriding , a subclass
object call its own variables and methods. Subclass cannot 
access the variables and methods of
superclass because the overridden variables or methods hides the 
methods and variables of super class.
But still java provides a way to access super class members even if 
its members are overridden. Super is
used to access superclass variables, methods, constructors.
Super can be used in two forms :
1) First form is for calling super class constructor.
2) Second one is to call super class variables,methods.
Super if present must be the first statement.

Example 3: super keyword in java

// example on super keyword in java
class Manager
{
   int salary = 50000;
}
class Employee extends Manager
{
   int salary = 20000;
   void display(int salary)
   {
      // here refers to the parent class instance variable
      System.out.println(super.salary);
   }
   public static void main(String[] args)
   {
      Employee obj = new Employee();
      obj.display(30000);
   }
}

Example 4: public class extends java super

public MountainBike(int startHeight, 
                    int startCadence,
                    int startSpeed,
                    int startGear) {
    super(startCadence, startSpeed, startGear);
    seatHeight = startHeight;
}

Example 5: how to use super class method in java

class Parentclass
{
   //no-arg constructor
   Parentclass(){
	System.out.println("no-arg constructor of parent class");
   }
   //arg or parameterized constructor
   Parentclass(String str){
	System.out.println("parameterized constructor of parent class");
   }
}
class Subclass extends Parentclass
{
   Subclass(){
       /* super() must be added to the first statement of constructor 
	* otherwise you will get a compilation error. Another important 
	* point to note is that when we explicitly use super in constructor
	* the compiler doesn't invoke the parent constructor automatically.
	*/
	super("Hahaha");
	System.out.println("Constructor of child class");

   }
   void display(){
	System.out.println("Hello");
   }
   public static void main(String args[]){		
	Subclass obj= new Subclass();
	obj.display();	 
   }
}

Tags:

Java Example