Showing posts with label super keyword in java wiki. Show all posts
Showing posts with label super keyword in java wiki. Show all posts

Use of super keyword in java

Monday, 12 November 2012

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
 

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