Saturday, 19 May 2018



super keyword in java


Case :1  if attribute are declared with same name inside parent class or  child class  ,We can differentiate  those attribute  using super keyword.

 package javaresearch;

public class ParentDemo {
               
                String name="ballabh khan";
               

}

package javaresearch;

public class ChildDemo extends ParentDemo {
               
                String name="govind";
               
                public void display()
                {
                                System.out.println(name);
                                System.out.println(super.name);
                }
               
                public static void main(String[] args) {
                                ChildDemo cd=new ChildDemo();
                                cd.display();
                }

}

o/p:------------------------
govind
ballabh khan



=======================================================================


Case:2  super can be used to invoke parent class method

package javaresearch;

public class ParentMethod {
               
                public void printName()
                {
                                System.out.println("Govind");
                }

}


package javaresearch;

public class ChildMethod extends ParentMethod {
               
                public void printName()
                {
                                System.out.println("ballabh khan");
                }
               
                public void printAddress()
                {
                                System.out.println("Hanumannagar Bhour Madhubani Bihar-847231");
                }
               
                public void printNameAddress()
                {
                               
                                super.printName();
                                printName();
                                printAddress();
                               
                }

public static void main(String[] args) {
                        ChildMethod cm=new ChildMethod();
                        cm.printNameAddress();
            }

}

o/p:-------------
Govind
ballabh khan
Hanumannagar Bhour Madhubani Bihar-847231
               
========================================================================


Case 3. super is used to invoke parent class constructor.


package javaresearch;

public class ParentConstructor {
           
            ParentConstructor ()
            {
                        System.out.println("Parent Counstructor");
            }

}


package javaresearch;

public class ChildConstructor extends ParentConstructor {
           
            ChildConstructor()
            {
                        super();
                        System.out.println("Child Constructor");
            }
           
            public static void main(String[] args) {
                        ChildConstructor cc=new ChildConstructor();
                       
            }

}

o/p:----------------------
Parent Counstructor
Child Constructor



 ======================================================================

Case 4:Another example of super keyword where super() is provided by the compiler implicitly.



package javaresearch;

public class ParentConstructor {
           
            ParentConstructor ()
            {
                        System.out.println("Parent Counstructor");
            }

}


package javaresearch;

public class ChildConstructor extends ParentConstructor {
           
            ChildConstructor()
            {
                       
                        System.out.println("Child Constructor");
            }
           
            public static void main(String[] args) {
                        ChildConstructor cc=new ChildConstructor();
                       
            }

}

o/p:----------------------
Parent Counstructor
Child Constructor
 ========================================================================================


Case 5:-super example: real use

Case :1  if attribute are declared with same name inside parent class or  child class  ,We can differentiate  those attribute  using super keyword.

Case:2  super can be used to invoke parent class method

Case 3. super is used to invoke parent class constructor.

Case 4:Another example of super keyword where super() is provided by the compiler implicitly.

 

package javaresearch;

public class Employee {
           
     int x=100;
           
            public Employee()
            {
                        System.out.println("Employee Constructor");
            }
           
            int empid;
            String empname;
            public Employee(int empid, String empname) {
                        super();
                        this.empid = empid;
                        this.empname = empname;
            }
           
            public void display()
            {
                        System.out.println("Empid="+empid);
                        System.out.println("EmpName="+empname);
            }

}





 package javaresearch;

public class Address  extends Employee{
           
            int x=200;
            String village;
            String district;
            String state;
           
            public Address()
            {   //super();
                        System.out.println("Address constructor");
            }
           
           

public Address(int empid, String empname, String village, String district, String state) {
                        super(empid, empname);
                        this.village = village;
                        this.district = district;
                        this.state = state;
            }
           
            public void display()
            {
                        super.display();
                        System.out.println("Village="+village);
                        System.out.println("District="+district);
                        System.out.println("State="+state);
                        System.out.println(x);
                        System.out.println(super.x);
            }
            public static void main(String[] args) {
                        Address ad=new Address();
                        Address add=new Address(101, "govind ballabh khan", "Hanumannagar", "madhubani", "bihar");
                        add.display();
            }
}

o/p:---------------------------
Employee Constructor
Address constructor
Empid=101
EmpName=govind ballabh khan
Village=Hanumannagar
District=madhubani
State=bihar
200
100
 =========================================================================












No comments:

Post a Comment