Could anyone say what is mean of super()

Asked By 7060 points N/A Posted on -
qa-featured

Could anyone say me where and why we used super() function?

SHARE
Best Answer by TennyPotter
Answered By 0 points N/A #80403

Could anyone say what is mean of super()

qa-featured

In Java, super() function is mainly used inside the constructor to invoke the constructor of parent  class (superclass).  We use the super() function to initialize the variables of the parent class or sometimes  we need to pass the parameters to the constructor of the parent class from  the derived class. 

For example: The following class make a call to super() function inside its constructor to invoke its superclass constructor in order to set the title of the created frame.

public class MyWindow extends JFrame {

  // Constructor

  public MyWindow(String title) {

     // invoke the constructor of superclass

    super(title)

  …

}

In this example, when a instance of the class MyWindow is created, its constructor will invoke the constructor of the parent class and pass the variable 'title' as its parameter.

Best Answer
Best Answer
Answered By 0 points N/A #80405

Could anyone say what is mean of super()

qa-featured

In Java super  keyword is used to point to the superclass instance. If you want to invoke a super class constructor or access a super class data from a derived class, you use the super keyword.

This example shows how to use super  to access the super class data inside a derived class.

class Base                                                                                     
{
int t = 10;
}
class Test extends Base
{
public void m()
{
  System.out.println(super.t);
}
}                                                                                     

In Java, the 'super' keyword is used to call the parent class's constructor, with arguments if desired, using the syntax
super(arg0, arg1, …). The restriction is that it must be the very first line in every constructor unless omitted, in which case
It will be automatically inserted without arguments

Related Questions