Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad practice to call super() in a class that doesn't extend anything?

Quite often I see people explicitly calling super() in a class that doesn't explicitly extend anything.

public class Foo
{
  public Foo()
  {
    super();

    //do other constructor stuff
  }
}

Now I know this is perfectly legal and if omitted the call is added by the compiler but I still think its bad practice. Whenever I see this I wonder if the programmer has some misunderstanding of inheritance and the fact that all classes implicitly extend Object.

Should I add this to our coding standards/best practice and should I pull up the other devs in my team when I see them do it? Its a personal bug-bear of mine but I don't know if I'm just being picky or not.

like image 281
Qwerky Avatar asked Jan 13 '11 11:01

Qwerky


People also ask

Is it necessary to call super ()?

However, using super() is not compulsory. Even if super() is not used in the subclass constructor, the compiler implicitly calls the default constructor of the superclass.

What happens if you don't call super Java?

If we call "super()" without any superclass Actually, nothing will be displayed. Since the class named Object is the superclass of all classes in Java. If you call "super()" without any superclass, Internally, the default constructor of the Object class will be invoked (which displays nothing).

Is super () a call?

super() is used to call Base class's(Parent class's) constructor.

Is super super allowed in Java?

Your code would break if you called a super of a super that had no super. Object oriented programming (which Java is) is all about objects, not functions. If you want task oriented programming, choose C++ or something else.


2 Answers

While I'm one whose all for consistency, I'm also against micromanaging every bit of style. Having a huge list of coding conventions, particularly when some of them seem arbitrary, is part of what discourages people from following them. I think coding guidelines should be streamlined to the most valuable practices that improve the -ilities. How much is readability, maintainability, performance, etc improved by mandating this practice?

While my personal practice is to not to call super() in such a case, its not serious enough that I would include it in the coding guidelines or call it a defect in reviewing other programmer's code. I would however still however mention it and discuss it in code reviews (not as a defect, just as a question of style) in an attempt to lobby more engineers to NOT use the call.

like image 99
Bert F Avatar answered Sep 22 '22 05:09

Bert F


Adding code that isn't needed is always a bad practice. It will make the code less readable as you would need to stop and start wondering why this line of code is added and if it is actually there for a reason.

like image 31
kgiannakakis Avatar answered Sep 20 '22 05:09

kgiannakakis