Monday, 3 October 2016

super keyword in java || When to use " super" and " this" keywords

The process of inheriting or extending one class features into another class is called inheritance. when a class extends another class, the newly created class is classed sub class and already existing class is class super class. In inheritance process we are always create object to sub class because the sub class contains both super class and sub class features.

              If super class and sub class have same member functions, them the sub class object calls only sub class version of the members.So, at this situation to call super class members we use " super" keyword in java. There are three uses of super keyword
  • To call the super class constructor
  • To call the super class variables
  • To call the super class methods.

Using super to call super class method

When we extend a class all the super class members are available in sub class. But, when both super and sub class contains same method, the always looks for sub class version of the method only. To call the hidden super class method, we use super keyword as

class one
{
    int i=10;
    void show()
    {
        System.out.println(" super class method: i =" + i);
    }
}

class Two extends one
{
    int i=20;
    void show()
    {
        System.out.println(" sub class method: i =" + i);
        super.show();
        System.out.println(" super class method:i=" + super.i);
    }
}

class superDemo
{
    public static void main(String args[])
    {
        Two t= new Two();
        t.show();
     }
}


Note: When your working super, in sub class the first statement must of super statement. Otherwise you will get an error.

Using super to call super class constructors and variables

     we need not access the default constructor of the super class, as it available to sub class by default. There four we call only the parameterized constructors using super. The following program show how to call super class parameterized constructors and variables.

class one
{
    int k=50;
    one( int a)
    {
        System.out.println(" super class constructor value is=" + a);
    }
}

class Two2 extends one
{      
        Two2()
        {
            super(5); // calling super class constructor
            System.out.println(" super class value is =" + super.k);  // accessing super class version of k.
        }

     void show()
    {
       
        System.out.println(" sub class method is called");
    }
}

class Super1
{
    public static void main(String args[])
    {
        Two2 t= new Two2();
        t.show();
       
    }
}

 Note:
          Sine in java every class inherit the super class Object class. That is object class is the super class for all the java classes that we are writing in our programs. So, the following code is valid.

Class mysubclass
{
       super();
  System.out.println(" Hello I am sub class of object super class");
}

Here, The super class call the default the constructor of the object class.

When to use " super" and " this" keywords 

If super class and sub class have same member functions, them the sub class object calls only sub class version of members.So, to over come this we use " super" keyword in java.

                Sine there are no objects at created and if you want to refer the current class object, then use the this keyword. Check here more details about this keyword

0 comments:

Post a Comment

Scribe to Youtube Channel

Advertisement

Find Us On Facebook

C Programming

More

C++ Tutorial

More

Java Tutorial

More

software engineering

More

MS Office

More

Database Management

More
Top