Super keyword in Java

Super Keyword in Java

1. super keyword is used to call parent class property or using super keyword we can remove confliction between parent class property and child class property.

        class Parent{

            String name="Parent Name";
        
        }   
        
        class Child extends Parent{
        
            String name="Child Name";
        
        
            public void printName(){
                System.out.println(name);
                System.out.println(super.name);
            }
        
            public static void main(String [] args){
        
               Child child=new Child();
               child.printName();
        
            }
        
        }
    
        Output is :
        Child Name
        Parent Name
    

In this program both class has same variable name so if child class want to call both name from non static context so we can use super keyword But if you want to access both name from static context so we can access through reference variable like below:

        class Parent{

            String name="Parent Name";
        
        }   
        
        class Child extends Parent{
        
            String name="Child Name";
        
            public static void main(String [] args){
        
               Child child=new Child();
               System.out.println(child.name);
               
               Parent parent=child;
               System.out.println(parent.name);
        
            }
        
        }
    
        Output is :
        Child Name
        Parent Name
    

2. using super keyword we can also call overridden property.

        class Parent{

            public void song(){
            System.out.println("Mukesh Song");
            }
            
            }   
            
            class Child extends Parent{
            
            public void song(){
            System.out.println("Arjit Song");
            }
            
            public void listen(){
                //if child want to listen Arjit song then they can call directly like below.
                song();
                //if child want to listen Mukesh song then they can call by super keyword like below.
                super.song();
            }
            
                public static void main(String [] args){
            
                   Child child=new Child();
                   child.listen();
            
                }
            
            }           
    
        Output is :
        Arjit Song
        Mukesh Song
    

3. we can never get value of super keyword.

        class Parent{


        }   
        
        class Child extends Parent{
        
        
        public void show(){
        System.out.println(super);
        }
        
            public static void main(String [] args){
        
               Child child=new Child();
               child.show();
        
            }
        
        }        
    
    Output is : Compiler time error
    

4. we can never use super keyword as an argument.

        class Parent{

            public void show(Parent p){
            System.out.println("Papa");
            }
            
            }   
            
            class Child extends Parent{
            
            
            public void call(){
            show(super);
            }
            
                public static void main(String [] args){
            
                   Child child=new Child();
                   child.call();
            
                }
            
            }            
    
    Output is : Compiler time error
    

5. we can never use super keyword as a return type.

        class Parent{

        }   
        
        class Child extends Parent{
        
        
        public Parent show(){
        return super;
        }
        
            public static void main(String [] args){
        
               Child child=new Child();
               child.show();
        
            }
        
        }        
    
    Output is : Compiler time error
    
Note : super keyword can never used in static context.

super() Constructor

1. super() is used to call parent class constructor.

                
class Parent {
	public Parent() {
		System.out.println("Hello Default");
	}
}

class Child extends Parent {
	public Child() {
		super();
	}
}

public class Demo {
	public static void main(String[] args) {
		new Child();
	}
}
        
    
        Output is :
        Hello Default
    

If you want to call parent class default constructor then we dont need to write super() explicit like below:

            
    
    class Parent {
    	public Parent() {
    		System.out.println("Hello Default");
    	}
    }
    
    class Child extends Parent {
    	public Child() {
    		
    	}
    }
    
    public class Demo {
    	public static void main(String[] args) {
    		new Child();
    	}
    }
            
        
        Output is :
        Hello Default
    

But suppose if parent class has more than one constructor so by default parent class default constructor will be called so if you want to call another constructor then we need to call super() explicit like below:


class Parent {

	public Parent() {
		System.out.println("Hello Default");
	}

	public Parent(int i) {
		System.out.println("Hello parameterized");
	}
    
}

class Child extends Parent {
	
    public Child() {
		super(10);
	}

}

public class Demo {
	
    public static void main(String[] args) {
		new Child();
	}

}

Output is :
Hello parameterized
    

2. Using super() constructor we can initialized parent class property also.

        
import java.util.Scanner;

class Parent {
	String name;

	Parent(String name) {
		this.name = name;
	}
}

class Child extends Parent {
	String name;

	Child(String name, String pname) {
		super(pname);
		this.name = name;
	}

}

public class Demo {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("Enter parent name");
		String pname = scanner.next();
		System.out.println("Enter child name");
		String cname = scanner.next();
		
		Child child = new Child(cname, pname);

		System.out.println(child.name);

		Parent parent = child; // Type Casting
		System.out.println(parent.name);

	}
}
        
    
Output is :
Enter parent name
parent
Enter child name
child
child
parent
    
Note: super() must be used inside a constructor and that must be the first statement.

No comments: