Is super()
used to call the parent constructor?
Please explain super()
.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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.
For example, in selenium automation, you have a PageObject which can use its parent's constructor like this:
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.
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 :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 :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.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
That is correct. Super is used to call the parent constructor. So suppose you have a code block like so
Then you can assign a value to the member variable n.