user3150201
user3150201

Reputation: 1947

Calling constructor of superclass doesn't work

In the code of a game I'm making, I have a class named Entity. This is the superclass of all graphical objects in the game (spaceships, missiles, etc).

Entity has an attribute int type. Entity receives a value for type in it's constructor (public Entity (int type)).

Class Ship extends Entity. It needs to have a value in type. This value is given to it also in it's constructor (public Ship (int type)). Inside the constructor of Ship, Eclipse told me to call the superclass' constructor (aka super() - Don't know why I have to do that). So I call super(type) from within Ship's constructor.

Aka:

public class Ship extends Entity{

    public Ship(int type){
        super(type);
    }

}

public class Entity{

    public Entity(int type){
        this.type = type;
    }

}

Later, when I check the value in type for an instance of Ship, I always get a 0, no matter what value was put in the constructor when the instance was built.

I guess that I'm supposed to do this.type = type inside Ship's constructor. But then, what's the point of inheritance? Isn't that supposed to take care of it?

Thanks

Upvotes: -1

Views: 1053

Answers (3)

keshlam
keshlam

Reputation: 8058

After discussion with the OP (see below), we've established that the actual problem was that there was a field named type in Ship, which was blocking visibility of the identically-named field in Entity. The obvious solution was to remove this field from Ship, allowing the superclass's field to be inherited as intended.

Upvotes: 1

Girish
Girish

Reputation: 1717

My friend in entity class you not have any default constructor by default the compiler sees you code like

public class Ship extends Entity{

    public Ship(int type){
        super();   ------> added by compiler by default
    }

}

if u not added call to the super it will added default call to super that's the reason of error, so you have to add super(type); in first line of derived class constructor because you have only parameter constructor.

Upvotes: 0

rgettman
rgettman

Reputation: 178333

Calling super(type) is a way of explicitly calling a superclass constructor that takes an argument. That is valid for your Ship class, calling the Entity constructor with the argument.

But Entity's superclass is Object (implicitly), and Object has a no-argument constructor, so there's no need to super() explicitly. In a constructor, Java automatically inserts a call to the no-arg superclass constructor if super() isn't present.

Also, Entity is the place that you actually need to process the argument. There's no need to pass type to Object, anyway.

Try

private int type;
public Entity(int type){
    this.type = type;
}

Handle the argument here in Entity, plus there's no need to call a superclass constructor explicitly.

Additionally, you may want to add a public getter method in Entity so that subclasses and other classes can access the type value.

Upvotes: 2

Related Questions