Abdhul Zaman
Abdhul Zaman

Reputation: 13

Constructor calling inherited constructor in java

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

}

Upvotes: 0

Views: 217

Answers (2)

Jeffrey
Jeffrey

Reputation: 44808

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

Upvotes: 0

Wyzard
Wyzard

Reputation: 34581

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.

Upvotes: 3

Related Questions