Reputation: 393
I have two classes as below. I want to access this() and super() constructors in single constructor TestEmployee(). Current method fails to compile. Is there any other way in Java so I can call both this() and super() in same constructor's body.
class Employee{
double salary;
Employee(double salary){
this.salary = salary;
}
}
class TestEmployee extends Employee{
TestEmployee(){
super(1000000);
this(10000);
}
double bonus;
TestEmployee(double bonus){
this.bonus = bonus;
}
}
Upvotes: 0
Views: 283
Reputation: 1
You can't put in the same constructor this two calls because both of them need to be put first in the constructor when calls the parent class. You can use this and super in the same constructor when you have an extra parameter. Like in this exemple:
class TestEmployee extends Employee{
double: age;
TestEmployee(double salary, double age){
super(1000000);
this.age=age;
}
Or when you are using a Chaining Constructor, for example:
public class Employee{
String name;
double salary;
double age;
public Employee()
{
this("James Bond", 34000);
}
public Employee(String n, double s)
{
this("Ana", 34000, 23);
}
public Employee(String n, double s, double age)
{
name = n;
salary = s;
this.age=age;
}
Upvotes: 0
Reputation: 6675
To prevent code duplication,you can define a non-static method which does all your initlization block for all your constructors.You can include this method below your call to super() to initialize your object.But the drawback is ..it can be called from any other method within the class.So it is not a good design pattern
class TestEmployee extends Employee{
double bonus;
TestEmployee(){
super(1000000);
intialize(10000);
}
TestEmployee(double bonus){
super(bonus);
intialize(bonus);
}
private void intialize(double bonus)
{
this.salary = bonus;
}
}
Upvotes: 0
Reputation: 2772
As others have said, you cannot do exactly what you want the way you're trying to approach it. What you could do, however, is design your classes a bit differently so that you can achieve the same goal.
class Employee{
double salary;
Employee(double salary){
this.salary = salary;
}
}
class TestEmployee extends Employee{
TestEmployee(double salary, double bonus){
super(salary);
this.bonus=bonus;
}
double bonus;
}
Then if you wanted a default TestEmployee constructor, you could do:
TestEmployee() {
this(1000000,10000);
}
Upvotes: 0
Reputation:
It would not make sense because TestEmployee
has a salary, which is passed in and sent to the base class Employee
because TestEmployee
derives from Employee
. A bonus is a separate part of compensation and would be stored separately. Ideally, you would have a different constructor in the Employee
base class which allowed for the passing of a salary
and a bonus
, which TestEmployee
could access.
Upvotes: 0
Reputation: 5663
As others have said, why would you even want to. It makes no sense.
You can fake it by calling another constructor which in turn calls its superclass constructor. But if the only reason to call another constructor is to relay to another superclass constructor than the one that gets called implicity there's almost certainly something seriously wrong with your design.
Upvotes: -1
Reputation: 6675
Obviously no,Both super and this must be the first statement of constructor body.And there can be only one first statement
Upvotes: 3