Gadgets

Wednesday, 9 March 2016

java program for super keyword

Write a java program to given the example for “super” keyword.


super() is used to invoke super class constructors and super keyword is refer super class members.
in this program i did show the difference with and without super keyword.
by using super keyword we can access parent class properties.

public class SuperTest {
       int number=10;
       SuperTest(){
        System.out.println("parent class constructor ");
       }     
}
class Test extends SuperTest{    
       int number=20;      
       Test(){
              super();
              System.out.println("using super keyword :" +super.number);
              System.out.println("with out using super keyword :" +number);
       }     
       public static void main(String[] args) {
              Test obj=new Test();      
       }
}

OUTPUT:

/*
parent class constructor
using super keyword :10
with out using super keyword :20
*/

No comments:

Post a Comment