For example
Class A does not have a default constructor.
Class B extends A

Any reason/benefits to do this?

public class B extends A
{
   public B()
   {
      super();
   }
}

Recommended Answers

All 4 Replies

All classes have a default constructor. If it's not present, then it's implicit.

The call to super is also implicit if it's not present. The benifit of calling super is good design practice. Each constructor calls it's super class until it reaches the top of the heiarchy(most likey Object class).

Calling super() yourself just means that the JVM won't have to worry about doing all that themeselves.

Ahh I see.
So it’s good design practice.
If this is done throughout a large program, would this have a positive impact on performance?
Since the JVM won’t have to worry about it.

Thanks

No not really. I don't think there's a performance hit at all, but it's excellent practice to include that call, and even I do it. I know I don't have to, but I do it anyway.

Yes it is the best practice to provide the default constructor in the program code that we write.

If we do not write one, the compiler includes a default one into the byte code. However there is no gaurentee that performance wise it is faster.

Having our own constructor is not really a place where we got to bother of performance as Java is slow anyway :cheesy:

______________________________________

Programming ( Assignment / Project ) Help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.