answersLogoWhite

0


Best Answer

Because, the parent class also needs to be initialized when you create an object in the inheritance hierarchy.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why super constructor should be first statement in subclass constructor in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When a subclass can call a parent's class constructor?

A subclass invokes its base class constructor at the point of instantiation. That is; you cannot instantiate a subclass object without first constructing its base class, which is done automatically.


When invoking a constructor from subclass its super class's no-arg constructor is always invoked?

Implicitly: (i.e., you do not code for it, but works as if you did)calling the no-argument constructor of the subclass, and there is no explicitly "redirect" codes.Explicitly:a constructor with base() / super() in the implementation, even that invoked constructor required some arguments.C# example: public SubClass(string whatever) : base() {//...}


What are the rules on how to construct java constructor?

1. The constructor has to have the same name as the classthat it is in.2. It does not have a return type. If it has a return type, then it is a method (even though it is legal, it's not ideal to have name a method the same name as the class).3. It can use any access modifier (this includes private).4. The default constructor does not take arguments.5. The first statement in a constructor has to have a super() type or this() type. If this is not written, by default, it's super(). It's illegal to have it in any other line other than the first line.6. Constructors can only access static variables.7. Only constructors have access to another constructor.Remember that interfaces do not have a constructor.


Why you should write 'this' keyword as a first statement in the method or any were?

no


Why we can't use super and this keywords in the same constructor?

because, the super refers to the constructor of the parent class while this refers to the constructor of the current class. Either statements must be the first line in a constructor. So, since we cannot have two first lines in a method we cannot have both keywords in the same constructor.public RandomTest() {super();this();}The above code will never compile. Even if we flip the positions of super and this, we will get the same compilation error.

Related questions

Why base class constructor is first invoked in subclass constructor?

The base class constructor is invoked first when a subclass is instantiated, because the base class must be viable and consistent before the subclass constructor is fired.


When a subclass can call a parent's class constructor?

A subclass invokes its base class constructor at the point of instantiation. That is; you cannot instantiate a subclass object without first constructing its base class, which is done automatically.


When invoking a constructor from subclass its super class's no-arg constructor is always invoked?

Implicitly: (i.e., you do not code for it, but works as if you did)calling the no-argument constructor of the subclass, and there is no explicitly "redirect" codes.Explicitly:a constructor with base() / super() in the implementation, even that invoked constructor required some arguments.C# example: public SubClass(string whatever) : base() {//...}


What is first call in struts2 default constructor or validate method?

Default Constructor will be called first . If you override Validate method , then validate method will be called .


What are the rules on how to construct java constructor?

1. The constructor has to have the same name as the classthat it is in.2. It does not have a return type. If it has a return type, then it is a method (even though it is legal, it's not ideal to have name a method the same name as the class).3. It can use any access modifier (this includes private).4. The default constructor does not take arguments.5. The first statement in a constructor has to have a super() type or this() type. If this is not written, by default, it's super(). It's illegal to have it in any other line other than the first line.6. Constructors can only access static variables.7. Only constructors have access to another constructor.Remember that interfaces do not have a constructor.


When did ferrari first win their first f1 constructor world championship?

Ferrari won their first F1 constructor world title in 1961 and have won it 16 times till last year


Who was the first constructor to reach 150 race victories?

ferrari


A thesis statement should be?

It should be a strong statement written in the first line of introduction, in a single sentence, stating what the topic is about.


What should a thesis statement be?

It should be a strong statement written in the first line of introduction, in a single sentence, stating what the topic is about.


What is the method of constructor overloading?

The first thing to note about constructor overloading is that Java creates a no argument constructor for you if and only if you have not typed a constructor yourself. Every class has a constructor even abstract ones (default no argument constructor). Abstract constructors are always executed. To overload a constructor you can do the following: class Test { String name; Test(String n) { name = n; System.out.println("Constructing Test Object named: " + name); } } In the case above we are overloading the default no argument constructor with a constructor that takes a String parameter. You can write you own no argument constructor as follows: class Test { Test() { System.out.println("Constructing Test Object"); } } To override our own no argument constructor we do this: class Test { Test() { // our no argument constructor System.out.println("Constructing Test Object"); } String name; Test(String n) { // overloading our no argument constructor with this // constructor that take a String parameter name = n; System.out.println("Constructing Test Object named: " + name); } }


Why you should write 'this' keyword as a first statement in the method or any were?

no


What should the first sentence of an essay say?

A general, relevant statement.