Sunday, 23 October 2016

super keyword

super keyword

We can apply on,
  1. To refer parent class instance variable
  2. To refer parent class method.
  3. To refer parent class constructor.
1. To refer parent class instance variable
  • If parent class and child classes having same variable names while accessing it will get ambiguity.
  • By default  only child class variable will be called, from child class if you want to call super class variable then we will call by super keyword.
--------------------------------------------------------------------------
Program         :       calling super class variable by super keyword
Program name  :       Demo1.java
Output            :        
                             child class variable:22
                             super class variable:11
class Parent
{
      int a=11;
}
class Child extends Parent
{
      int a=22;
      public void method1()
      {
           System.out.println("child class variable:"+a);
           System.out.println("super class variable:"+super.a);
      }
}
class Demo1
{
      public static void main(String args[])
      {
           Child c=new Child();
           c.method1();
      }
}

Compile          :      javac Demo1.java
Run                :      java Demo1
Output            :        
                            child class variable:22
                            super class variable:11
--------------------------------------------------------------------------
2. To refer parent class method
  • If parent class and child classes having same method names while accessing it will get ambiguity.
  • By default  only child class method will be called, from child class if you want to call super class method then we will call by super keyword.
--------------------------------------------------------------------------
Program         :       calling super class method by super keyword
Program name  :       Demo2.java
Output            :        
                             child class variable:22
                             super class variable:11
class Parent1
{
      public void method1()
      {
           System.out.println("super class method");
      }
}
class Child1 extends Parent1
{
      public void method1()
      {
           System.out.println("child class method");
           super.method1();
      }
}
class Demo2
{
      public static void main(String args[])
      {
           Child1 c=new Child1();
           c.method1();
      }
}

Compile          :      javac Demo2.java
Run                :      java Demo2
Output            :        
                            child class method
                            super class method
--------------------------------------------------------------------------
3. To refer parent class constructor
  • If we want to call super class constructor from child class constructor, they by using super keyword we can call.
--------------------------------------------------------------------------

Program         :       calling super class constructor by super keyword
Program name  :       Demo3.java
Output            :        
                             child class variable:22
                             super class variable:11
class Parent2
{
      Parent2(int x)
      {
           System.out.println("super class constructor");
      }
}

class Child2 extends Parent2
{
      Child2()
      {
           super(10);
           System.out.println("child class constructor");
      }
}

class Demo3
{
      public static void main(String args[])
      {
           Child2 c=new Child2();
      }
}

Compile          :      javac Demo3.java
Run                :      java Demo3
Output            :        
                            super class constructor
                            child class constructor
--------------------------------------------------------------------------
Thanks for your time.
Nireekshan