Should I create builders for all inherited java classes?

advertisements

My base class has a constructor with parameters (String, String, int) Three different classes inherit from it. Inherited class A has a constructor with parameters (String, String, int) too but with different implementation.

My question is if I do not make a constructor in inherited classes B and C with parameters (String, String, int) will they inherit the base class' constructor ? I need the same implementation in B and C as in the base class.


If you don't specify a constructor in base class B and C, you will get the default constructor. This is a no parameter constructor provided by the runtime, and you can use it for any Object without an explicit constructor defined.

If you want to access a superclass constructor, you can do something like.

public A(String str, String str2, int num)
{
    super(str, str2, num);
}

Extra Reading

  • You can read about the use of the super keyword here.

  • You can read about what get's inherited in Java here.