-2

We use super() to call the constructor of parent class. we know that every method have:

  1. some prototype (i.e. modifier returnType methodName (arguments){}),
  2. Its own definition, and
  3. In which class it is present.

So, I want to know only that in which class super() is present and what its prototype.

class ParentClass
{
    ParentClass()
    {
        System.out.println("parent class constructor");
    }
}


public class ChildClass extends ParentClass
{
    ChildClass()
    {
        super();
    }

    public static void main(String[] a)
    {
        ChildClass cc = new ChildClass();
    }
}

Output parent class constructor

super() is not present in our class and Object class then how constructor internally call the super()?

  • 7
    *Prototype* isn't a thing in Java. I think you mean the **signature** of methods here. – GhostCat Sep 01 '19 at 05:17
  • "*Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure: ... 3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using `this`). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using `super`)."* - [JLS §12.5](https://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.5). Amen. – Turing85 Sep 01 '19 at 06:20

2 Answers2

2

super() is not present in our class and Object class then how constructor internally call the super()?

Consider super() to be more of keyword. It is not a method name.

In other words: it doesn't denote a method named super. It denotes (for example) a constructor from the super class.

In your example, that call to super simply "points" to that ParentClass() constructor.

See here or there for more details.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Sir can you explain: "Consider super() to be more of keyword. It is not a method name" in detail. if super() is not method then why every we call it as super method – Yogendra Kumar Rajput Sep 01 '19 at 08:14
  • @YogendraKumarRajput Well. Did you carefully read the two links I provided to you? You see, the point of this community is not to explain same things over and over again. Those links were also meant as inspiration: you ask for very basic things, and there are many existing resources explaining all details a new Java programmer might care about... – GhostCat Sep 01 '19 at 12:20
  • Sir these two links describe about the working of Super(). I know the working of super(). i want to know that- "How super() works,how super() directly call the parentClass constructor". And sir You says that -"super() to be more of keyword,it is not a method name". At last my query is how super() work. – Yogendra Kumar Rajput Sep 01 '19 at 12:41
  • Again: it is a keyword that tells the **compiler** which super class constructor to call. That is all there is to this. – GhostCat Sep 01 '19 at 13:00
  • okk now i got it, super() is looks like a method but actually not a method. like keywords super() have its own value, and tell the compiler to call super class constructor. – Yogendra Kumar Rajput Sep 01 '19 at 13:39
  • Now i can tell everyone Confidently that super() is not a method but actually like a keyword. Is this all also apply for this()? – Yogendra Kumar Rajput Sep 01 '19 at 13:41
  • Sir i am having a simple doubt, if super() is not a method then why we all says that it is a super method, instead of saying it is a special type of keyword. – Yogendra Kumar Rajput Sep 01 '19 at 14:09
  • Because, in the end, that is the point: calling a method of a super class. You get too hung up on words. Understand the concept, and accept that human language isn't always 100 precise. It is just more pragmatic to think about super that way. – GhostCat Sep 01 '19 at 15:25
0
  1. If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the superclass does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
  2. Either you write super() or not write in constructor of subclass, implicitly there is super() invoked to call superclass default constructor , you might think that a whole chain of constructors called, all the way back to the constructor of Object. This, in fact, is the case. It is called constructor chaining.

/* superclass Person */

class Person 
{ 
    Person() 
    { 
        System.out.println("Person class Constructor"); 
    } 
} 

/* subclass Student extending the Person class */
class Student extends Person 
{ 
    Student() 
    { 
        // invoke or call parent class constructor 
        super(); 

        System.out.println("Student class Constructor"); 
    } 
} 

/* Driver program to test*/
class Test 
{ 
    public static void main(String[] args) 
    { 
        Student s = new Student(); 
    } 
} 

Output:

Person class Constructor

Student class Constructor

Krishna Vyas
  • 1,009
  • 8
  • 25