answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why we can't use super and this keywords in the same constructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is constructor overloading same as method overloading.yes or no. explain?

yes,because in constructor overloading constructor have same and different parameter list. In method overloading method have same name and different parameter list.


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 we use super in java?

The keyword super is used to refer to the parent class instance of the current class. Lets say we have a class class A extends B { ... public void getName(){ } ... } Lets assume the parent class B also has a method getName() Inside class A if you call getName() it would by default call the current class's method. To make the JVM intentionally call the super class method we can use super if we say super.getName() then the parent class instance of the method would be called.


How do you get a default constructor?

The default constructor is an empty (only call the super constructor) with no parameters constructor inserted by the java compiler when you don't define a constructor in your class. If you write something like this: public class NoConstructorClass{ //no constructor goes here } Then you get something like this: public class NoConstructorClass{ public NoConstructorClass(){ // Default constructor that you didn't write super(); } }


Why you give the constructor name same as class name?

Constructor is just like method in class.it actually used for intialising members of class, lets consider ex. class construct { constrct(){} } in main() you hav to provide.. construct c1=new construct(); new construct() means calling the method of class,and which is used to initailise members of same class implicitly. this it is necessary for constructor to have same name

Related questions

Why am I getting Java unreported exception while compiling my class?

It may be because you are using inheritance and your super class constructor has a throws clause in its declaration. If your super class has a throws clause in its constructor declaration, you must do the same in your child class constructor in order to eliminate this compiler error.


Is constructor overloading same as method overloading.yes or no. explain?

yes,because in constructor overloading constructor have same and different parameter list. In method overloading method have same name and different parameter list.


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.


What is constructor in cplusplus?

A constructor is a special member function which have same name as the class name.`


What is conustrutor?

A constructor is a function in C which has the same name of the class. The constructor can be used to initialize some function.


How can you recognize a constructor in a class?

A class's constructor will have the same name of the class and no return type (not even void): class Example(){ Example() {printf("This is the constructor\n");} ~Example(){printf("This is the destructor\n");} };


Differene betwee constructor overloading same as method overloading?

A constructor is just a special form of a method. You can overload constructors in the exact same way as you can overload any other method.


Why we use super in java?

The keyword super is used to refer to the parent class instance of the current class. Lets say we have a class class A extends B { ... public void getName(){ } ... } Lets assume the parent class B also has a method getName() Inside class A if you call getName() it would by default call the current class's method. To make the JVM intentionally call the super class method we can use super if we say super.getName() then the parent class instance of the method would be called.


How do you get a default constructor?

The default constructor is an empty (only call the super constructor) with no parameters constructor inserted by the java compiler when you don't define a constructor in your class. If you write something like this: public class NoConstructorClass{ //no constructor goes here } Then you get something like this: public class NoConstructorClass{ public NoConstructorClass(){ // Default constructor that you didn't write super(); } }


Why you give the constructor name same as class name?

Constructor is just like method in class.it actually used for intialising members of class, lets consider ex. class construct { constrct(){} } in main() you hav to provide.. construct c1=new construct(); new construct() means calling the method of class,and which is used to initailise members of same class implicitly. this it is necessary for constructor to have same name


Should the constructor name and class name be same?

Every class object is created using the same new keyword, so it must have information about the class to which it must create an object. For this reason, the constructor name should be the same as the class name. To learn more about data science please visit- Learnbay.co


What is return data type?

A Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.