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.
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.
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).
super() is used to call Base class's(Parent class's) constructor.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With