Use of super keyword in java


The super keyword acts some what like this ,except that it always that it always refers to the super class of the subclass in which it is used.This usage has the following general form

super.member

member can be either a method or an instance variable.  the super keyword is most applicable to situations in which memebe names of a subclass hide memebers by the same name in the superclass .Consider this simple class hierarchy


class Base
{
  int i;
}
class SubClass extends Base
 {
  int i; // this i hides the i in A
  SubClass(int a, int b) {
    super.i = a; // i in A
    i = b; // i in B
  }
  void show()
  {
    System.out.println("i in superclass: " + super.i);
    System.out.println("i in subclass: " + i);
  }
}
public class Mainkey
{
  public static void main(String args[])
  {
    SubClass subOb = new SubClass(1, 2);
    subOb.show();
  }
}
Output 

G:\>javac Mainkey.java
G:\>java Mainkey
i in superclass: 1
i in subclass: 2

Use of super keyword in java

2 comments:

Unknown said...

That's really massive exposure post and I must admire you in this regard.

PIC Scheme Singapore

hitesh kumar said...

Super keyword in java

Thanks, keep sharing

Post a Comment

 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org