Constructor calling inherited constructor in java

2019-03-04 03:58发布

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?
   }

}

2条回答
Deceive 欺骗
2楼-- · 2019-03-04 04:38

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);.

查看更多
女痞
3楼-- · 2019-03-04 04:52

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.

查看更多
登录 后发表回答