Constructor calling inherited constructor in java

2019-03-04 04:23发布

问题:

How would I go about doing this in java?

I need the constructor in my subclass to call the inherited constructor in my superclass. is there a special syntax that i need to use in order to do this?

I've used extends to inherit from the Person class. How would i go about using super()?

Here is my class:

public class Student extends Person
{
   protected int id;

   public Student()
   {
   // how do i call the inherited constructor here?
   }

}

回答1:

super(arg1, arg2, etc);

There's an implicit call to super() (with no arguments) at the beginning of any constructor that doesn't call it explicitly.



回答2:

Exactly like that: super(). But I assume you need to call a method with arguments since there's an implicit call to super(). Add arguments, like this: super(arg0, arg1, arg2, etc);.