super() in Java

2018-12-31 10:18发布

Is super() used to call the parent constructor? Please explain super().

标签: java super
15条回答
看风景的人
2楼-- · 2018-12-31 10:53

The super keyword can be used to call the superclass constructor and to refer to a member of the superclass

When you call super() with the right arguments, we actually call the constructor Box, which initializes variables width, height and depth, referred to it by using the values of the corresponding parameters. You only remains to initialize its value added weight. If necessary, you can do now class variables Box as private. Put down in the fields of the Box class private modifier and make sure that you can access them without any problems.

At the superclass can be several overloaded versions constructors, so you can call the method super() with different parameters. The program will perform the constructor that matches the specified arguments.

public class Box {

    int width;
    int height;
    int depth;

    Box(int w, int h, int d) {
        width = w;
        height = h;
        depth = d;
    }

    public static void main(String[] args){
        HeavyBox heavy = new HeavyBox(12, 32, 23, 13);
    }

}

class HeavyBox extends Box {

    int weight;

    HeavyBox(int w, int h, int d, int m) {

        //call the superclass constructor
        super(w, h, d);
        weight = m;
    }

}
查看更多
刘海飞了
3楼-- · 2018-12-31 10:58

For example, in selenium automation, you have a PageObject which can use its parent's constructor like this:

public class DeveloperSteps extends ScenarioSteps {

public DeveloperSteps(Pages pages) {
    super(pages);
}........
查看更多
君临天下
4楼-- · 2018-12-31 11:00

I have seen all the answers. But everyone forgot to mention one very important point:

super() should be called or used in the first line of the constructor.

查看更多
美炸的是我
5楼-- · 2018-12-31 11:02

Constructors
In a constructor, you can use it without a dot to call another constructor. super calls a constructor in the superclass; this calls a constructor in this class :

public MyClass(int a) {
  this(a, 5);  // Here, I call another one of this class's constructors.
}

public MyClass(int a, int b) {
  super(a, b);  // Then, I call one of the superclass's constructors.
}

super is useful if the superclass needs to initialize itself. this is useful to allow you to write all the hard initialization code only once in one of the constructors and to call it from all the other, much easier-to-write constructors.

Methods
In any method, you can use it with a dot to call another method. super.method() calls a method in the superclass; this.method() calls a method in this class :

public String toString() {
  int    hp   = this.hitpoints();  // Calls the hitpoints method in this class
                                   //   for this object.
  String name = super.name();      // Calls the name method in the superclass
                                   //   for this object.

  return "[" + name + ": " + hp + " HP]";
}

super is useful in a certain scenario: if your class has the same method as your superclass, Java will assume you want the one in your class; super allows you to ask for the superclass's method instead. this is useful only as a way to make your code more readable.

查看更多
初与友歌
6楼-- · 2018-12-31 11:02

Super keyword in java

Super keyword is a reference variable that is used for refer parent class object.

Why use super keyword in java ?

Whenever inherit base class data into derived class it is chance to get ambiguity, because may be base class and derived class data are same so to difference these data need to use super keyword

Super keyword at method level ?

Super keyword is also used to call parent class method. Super keyword should be use in case of method overriding.

When use Super keyword at method level ?

Super keyword use when base class method name and derived class method name have same name.

http://www.tutorial4us.com/java/java-super-keyword

查看更多
回忆,回不去的记忆
7楼-- · 2018-12-31 11:04

That is correct. Super is used to call the parent constructor. So suppose you have a code block like so

class A{
    int n;
    public A(int x){
        n = x;
    }
}

class B extends A{
    int m;
    public B(int x, int y){
        super(x);
        m = y;
    }
}

Then you can assign a value to the member variable n.

查看更多
登录 后发表回答