Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Can I extend a class which has only a parametrized constructor in Java?

0
Posted

Can I extend a class which has only a parametrized constructor in Java?

0

You can but you need to explicitly call the base class parameterized constructor. You need to write like this: public class SubClass extends BaseClass{ public SubClass(){ super(null); } } Even when you are writing a sub class parameterized constructor, you need to explicitly refer to base class parameterized constrctor something like this: public SubClass extends SuperClass{ public SubClass(String name){ super(name); } } This is because, the java compiler tries to place the code in your costructor which will call base class’s Default constructor, as we don’t have the base class default constructor, we get the compilation error.

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123