Nitzan Farhi
Nitzan Farhi

Reputation: 175

java calling method before super

Assuming I have a super class that has 3 parameters in it's constructor and i am inheriting this class that also has a constructor with 3 parameters, and I want to call the super class constructor but before I want to do some logic on the sub class first, I can call a static method that receives those 3 parameters but I have to return only one, so this is the solution I came up with

public class someClass extends  SuperClass {
    public someClass(int a,int b,int c) {
         super(func(a,b,c),b,c);
    }
    public static int func(int a,int b,int c){
        //usage a b c
        return a;
    }
}

It seems a bit ugly and I was wondering if there is a better solution to use the parameters myself and then call super regularly. Note that i cannot change the Super class or that usages of the sub classes and therefore factory Design Pattern

Upvotes: 3

Views: 2750

Answers (3)

Robby Cornelissen
Robby Cornelissen

Reputation: 97322

To get the logic out of your constructor, you can easily create a factory method in your subclass:

public class SomeClass extends SuperClass {
    private SomeClass(int a, int b, int c) {
        super(a, b ,c);
    }

    public static SomeClass create(int a, int b, int c) {
        // calculate a for constructor
        
        return new SomeClass(a, b, c);
    }
}

Then you can create instances as follows:

SomeClass someClass = SomeClass.create(1, 2, 3);

Upvotes: 10

lance-java
lance-java

Reputation: 28061

You could also use the builder pattern

public class SomeClass extends SuperClass {
    public static class Builder {
        private int a, b, c;
        public Builder withA(int a) { 
            this.a = a;
            return this;
        }
        public Builder withB(int b) { ... }
        public Builder withC(int c) { ... }
        public SomeClass build() {
            // logic goes here
            return new SomeClass(...)
        }
    }

    // hide this from public use, use Builder instead
    protected SomeClass(int a, int b, int, c) {
        super(a, b, c);
    }
}

SomeClass someClass = new SomeClass.Builder().
    withA(1).
    withB(2).
    withC(3).
    build();

Upvotes: 1

C-Otto
C-Otto

Reputation: 5843

In Java you are not allowed to execute another statement before the call to super. The trick you mentioned works, but you cannot refactor your code to have the call to func in a statement before the call to super.

In my experience, issues like this often hint at some design issue. Maybe you can solve the underlying problem by re-thinking about the responsibilities of the two involved classes.

Upvotes: 4

Related Questions