What is the use of super keyword. give me an example

Questions by Sivasubramaniam   answers by Sivasubramaniam

Showing Answers 1 - 3 of 3 Answers

vballari

  • Oct 6th, 2006
 

With the keyword 'super', you can access the overridden methods and data.

public class A {
  int i = 0;
  void fun(){
     
i = 9;
  }
}
class B extends A {
  int j = 0;
  void fun() {
    j = 8;
    super.fun ();
    j = j - i;
  }
}

  Was this answer useful?  Yes

chaitanya

  • Oct 25th, 2006
 

the super keyword is used to access the method that belongs to parent class that has been overridden in the child class .

for example:

class a

{

              void method1(){statements}

}

class b extends a

{

            void method1(){statements}

             public static void main(Strings args[]){super.method1()}

}

 

 

  Was this answer useful?  Yes

Radhakrishna

  • Nov 8th, 2006
 

hi,

Totally there 3 uses of "super" keyword..  we can call the superclass variables,constructors and methods.. the belove example will give u the better understanding of super keyword..

class superclass
{
 public superclass(){
   System.out.println("in the super class constructor");
 }
  int x=10;
     public void y(){
   System.out.println(" in the superclass y() method");
  }
}
class subclass extends superclass
{
 public subclass(){
  super();
  System.out.println("in the subclass constructor");
 }
 public void y(){
  super.y();
  System.out.println("value of x is" + super.x);
  System.out.println("In the sub class y() method");
 }
}

public class Test6
{
 public static void main(String[] args){
  subclass obj=new subclass();
  obj.y();
 }
 
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions