4

I have a quick question regarding how super() is used in a constructor. I know the basics about how it will call a superclass but I recently looked at some code and don't understand how it is being used in this example. Here's the gist of the part that confuses me:

public class MyClass implements MyInterface {
    String myString = null;

    public MyClass() {
        super();
    }

    public MyClass(String A) {
        super();
        myString = A;
    }

    public interfaceMethod {
        // this is the method from MyInterface
    }
}

I understand how the constructors are being used and all but I just don't see the point to the super() methods in them. Does it have to do with the interface that it's implementing? I thought super() was used if a class extends something? Am I missing some basic knowledge about java here? Any help for a noob would be much appreciated. Thanks!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
John Octavious
  • 221
  • 1
  • 3
  • 9

4 Answers4

3

In this case super() refers to java.lang.Object 's default constructor, all object implicitly extends Object

jmj
  • 237,923
  • 42
  • 401
  • 438
1

Any java class inherits from java.lang.Object by default implicitly, hence above code snippet will call the constructor of Object class (which currently does nothing).

Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
  • Thank you for the response. Am I correct in assuming the super() here has nothing to do with the interface? Am I also correct in assuming getting rid of the super()'s in each constructor will really not have any impact on the code? – John Octavious Jun 25 '14 at 17:20
  • 2
    You are correct. The MyClass() constructor is a no-argument constructor. In fact it was not necessary to call super() at all since a no-arg constructor automatically calls super() as the first thing it does. The constructor body could have been empty. There's a good discussion of all this at http://www.javaworld.com/article/2076204/core-java/understanding-constructors.html – mister270 Jun 25 '14 at 17:32
0

In inheritance, when child class gets instantiated, the default(zero argument) constructor of base class gets invoked, regardless of presence of super() in child class constructor. However, if you want to invoke any other constructor of parent, you can call it using super(with arguments). In short, Super() is used alter the patterns of instantiations of parent classes.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

All classes in java directly or indirectly extends java.lang.Object.

Example if we write class A Compiler will do something like class A extends Object.

Suppose

class A extends B {}

then this is interpreted as,

class B extends Object {}
class A extends B {}

It is said that a class cannot be initialized until the object class initialize its part. Hence it is the responsibility of the compiler to invoke the Object class constructor. The compiler does so by writing super() in your constructor as the first statement.So even if you did not create any default constructor compiler will create one and write super() to it.

In this case when you initialize the MyClass without any parameter it calls the default constructor which see super() in it so it calls the Object class constructor. When you initialize the MyClass with the String parameter it calls the parameterized constructor which see super() in it so it calls the Object class constructor.

I hope I answered your query.

SparkOn
  • 8,806
  • 4
  • 29
  • 34