1

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.

Jan Kl.
  • 69
  • 2
  • 13
  • Possible duplicate of [Why do this() and super() have to be the first statement in a constructor?](https://stackoverflow.com/questions/1168345/why-do-this-and-super-have-to-be-the-first-statement-in-a-constructor) – JEY May 07 '18 at 13:21
  • 3
    *(Because I will be creating possibly hundreds/thousands of those extended classes.)* then you're doing something horribly wrong. – Lino May 07 '18 at 13:24
  • why don't you try to create an abstract class and extend it – Kushagra Misra May 07 '18 at 13:24
  • What is your goal? Maybe you're trying something out, that can be made much easier. (Because creating hundreds of different subclasses sounds really suspicios – Lino May 07 '18 at 13:25
  • 1
    If you need hundreds or thousands extensions of a specific class, then the implementation is likely faulty, based on the OOP paradigm. – Compass May 07 '18 at 13:31
  • @Lino My goal is to create classes that run different automated tests (or parts of these tests). I want to write the name of the "Test set" automatically - when its instance is created - instead of having to call a method for that every single time. – Jan Kl. May 07 '18 at 13:33
  • 2
    Are you trying to reinvent the wheel? This is what frameworks like JUnit are for. – Mark Rotteveel May 07 '18 at 13:37

3 Answers3

4

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.

Lino
  • 19,604
  • 6
  • 47
  • 65
2

Yes.

Otherwise, how will it know that you want to pass name into it and not some other parameter?

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
1

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.

Bohdan Levchenko
  • 3,411
  • 2
  • 24
  • 28
  • Ha! This seems exactly like what I need. I will take a look at the "abstracts" (never worked with it) and try it out. – Jan Kl. May 07 '18 at 13:41