Reputation: 89
Is there any way I can trigger class constructor automatically while extending from the class?
I have a class TestSet.java
public class TestSet {
public TestSet(String name) {
Logger.msg("Test set: " + name);
}
}
And I would like for the constructor to trigger every time that I extend from this class.
It seems that I need to call the constructor "manually" with:
public class TC1SendAnEmail extends TestSet{
//I have to type this every single time again and again...
//---------------------------------------------------------
public TC1SendAnEmail(String name) {
super(name);
}
//---------------------------------------------------------
public void run() {
new EmailLogin().run();
...
}
}
Which I would like to avoid. (Because I will be creating possibly hundreds/thousands of those extended classes.)
From what I have managed to research, I guess that this function is not implemented in Java. But it just seems weird that I would have to "copy-paste" the constructor again, again and again...
Maybe there is another solution that I dont see? (Maybe without even using the constructor to "do something every time an instance of a class that extends my TestSet class is created".)
EDIT: Yes, I can see why you think that creating hundreds/thousands of subclasses is wrong. I am creating a big automation project. Every class of this type will be a "test". And there will be thousands of tests...
EDIT#2: The point of this question was that I needed to trigger the superclass constructor every time I extend from it. My mistake was adding parameter to the superclass constructor. If you don´t add a parameter to the constructor, then it is triggered automatically while extending.
Upvotes: 1
Views: 1143
Reputation: 19910
From your comments I kinda understand what you're trying to achieve. (Automatically printing the name of the current running test)
So what about the following snippet:
public class TestSet {
public TestSet(){
Logger.msg("Test set: " + getClass().getSimpleName());
}
}
This baseclass just prints the name of the implementing class when it is created. E.g. when using the following class:
public class TC1SendAnEmail extends TestSet {
// your methods
}
it prints:
Test set: TC1SendAnEmail
This works, because in java the default constructor (constructor with no arguments) of the superclass doesn't have to be overridden, because the compiler will generate it automatically.
Upvotes: 4
Reputation: 3561
What you can do is replace this copy-paste code with some sort of Template method pattern:
public class SO50215241 {
public abstract static class TestSet {
public TestSet() {
System.out.println("Test set: " + getName());
}
abstract String getName();
}
public static class TC1SendAnEmail extends TestSet{
@Override
String getName() {
return "TC1Name";
}
}
public static void main(String[] args) {
new TC1SendAnEmail();
}
}
Prints:
Test set: TC1Name
Then you can try to extend some specific version of TestSet
for concrete implementations instead of extending TestSet
itself.
Or calculate name dynamically inside getName
method body.
Upvotes: 1
Reputation: 19453
Yes.
Otherwise, how will it know that you want to pass name
into it and not some other parameter?
Upvotes: 2