Super Keyword in Java

Advertisements

Java Super Keyword

Super keyword is used to pass values from derived class constructor to base class constructor or data members.

We can use super keyword in three ways.

  • Super for calling base class constructor
  • Super for explicitly passing value to base class data members
  • Super to invoke base class method in derived class

	Note: super() must be first statement in derived class constructor.

Super For Calling Base Class Constructor From Derived Class Cons.

Example of super keyword


//SuperCallingDemo.java

    class Employee
    {

        int Id;
        String Name;
        double Salary;

        Employee(int id,String name,double salary)
        {
            Id = id;
            Name = name;
            Salary = salary;
        }

        void PutData()
        {
            System.out.print("\n\n\tEmployee Id : " + Id);
            System.out.print("\n\tEmployee Name : " + Name);
            System.out.print("\n\tEmployee Salary : " + Salary);
        }
    }

    class Company extends Employee
    {
        int RegNo;
        String CName;

        Company(int regno,String cname,int id,String name,double salary)
        {
            super(id,name,salary);      // Statement 2

            RegNo = regno;
            CName = cname;
        }

        void WriteData()
        {
            System.out.print("\n\tRegistration No. : " + RegNo);
            System.out.print("\n\tCompany Name : " + CName);
        }
    }

    class SuperCallingDemo
    {
        public static void main(String args[])
        {
            Company C = new Company(715,"SIT",1,"Kumar",45000); // Statement 1
                            // Passing Values to Derived Class Constructor

            C.PutData();
            C.WriteData();
        }
    }

    Output :

                Employee Id : 1
                Employee Name : Kumar
                Employee Salary : 45000
                Registration No. : 715
                Company Name : SIT

In the above example, statement 1 is creating an object of Company class and passing values to Company's parameterized constructor.

Company's parameterized constructor will receive all the values and super keyword in statement 2 will invoke and pass values to Employee's class parameterized constructor.

Super For Explicitly Passing Values To Base Class Data Members

Super keyword can be used for explicitly passing values from derived to base class, when there is no parameterize constructor in base class as shown in given example.

Example of super keyword


//SuperExplicitDemo.java

    class Employee
    {

        int Id;
        String Name;
        double Salary;

        void PutData()
        {
            System.out.print("\n\n\tEmployee Id : " + Id);
            System.out.print("\n\tEmployee Name : " + Name);
            System.out.print("\n\tEmployee Salary : " + Salary);
        }
    }

    class Company extends Employee
    {
        int RegNo;
        String CName;

        Company(int regno,String cname,int id,String name,double salary)
        {
            super.Id = id;          // Statement 2
            super.Name = name;      // Statement 3
            super.Salary = salary;  // Statement 4

            RegNo = regno;
            CName = cname;
        }

        void WriteData()
        {

            System.out.print("\n\tRegistration No. : " + RegNo);
            System.out.print("\n\tCompany Name : " + CName);

            System.out.print("\n\tDisplaying base class data members using super keyword");
            System.out.print("\n\n\tEmployee Id : " + super.Id);
            System.out.print("\n\tEmployee Name : " + super.Name);
            System.out.print("\n\tEmployee Salary : " + super.Salary);

        }
    }

    class SuperExplicitDemo
    {
        public static void main(String args[])
        {
            Company C = new Company(715,"SIT",1,"Kumar",45000); // Statement 1
                            // Passing Values to Derived Class Constructor

            C.PutData();
            C.WriteData();
        }
    }

    Output :

                Employee Id : 1
                Employee Name : Kumar
                Employee Salary : 45000
                Registration No. : 715
                Company Name : SIT

                Displaying base class data members using super keyword
                Employee Id : 1
                Employee Name : Kumar
                Employee Salary : 45000


In the above example, statement 1 is creating an object of Company class and passing values to Company's parameterize constructor.

In statements 2, 3 and 4, super acts somewhat like this keyword, but the difference is, this refers to the current class in which it is used and super refers to the base class.

Super To Invoke Base Class Method From Derived Class

Super keyword can be used to invoke base class method from derived method using (.) dot operator as shown in given example.

Example of super keyword with method


//SuperMethodDemo.java

    class Employee
    {

        int Id;
        String Name;
        double Salary;

        Employee()
        {
            Id = 1;
            Name = "Kumar";
            Salary = 15000;
        }

        void PutData()
        {
            System.out.print("\n\n\tEmployee Id : " + Id);
            System.out.print("\n\tEmployee Name : " + Name);
            System.out.print("\n\tEmployee Salary : " + Salary);
        }
    }

    class Company extends Employee
    {
        int RegNo;
        String CName;

        Company()
        {
            RegNo = 715;
            CName = "TutorialDost";
        }

        void WriteData()
        {

           super.PutData()                    // Statement 1 : Calling base class method

            System.out.print("\n\tRegistration No. : " + RegNo);
            System.out.print("\n\tCompany Name : " + CName);

        }
    }

    class SuperMethodDemo
    {
        public static void main(String args[])
        {
            Company C = new Company();

            C.PutData();
            C.WriteData();
        }
    }

    Output :

                Employee Id : 1
                Employee Name : Kumar
                Employee Salary : 15000
                Registration No. : 715
                Company Name : TutotrialDost

In the above example, statement 1 is calling base class method using super keyword..

Advertisement