Pendo826
Pendo826

Reputation: 1002

Inheritance not working

Hey im just practicing inheritance and i encountered a problem. Im getting an error in my car class(sub-class) that the variables in Vehicle(parent) are not visible. i didnt do anything to change this and i dont even know how to make it invisible. Can anyone help me with this.

public class Vehicle 
{
    private String make, model, colour;
    private int registrationNumber;

    public Vehicle()
    {
        this.make = "";
        this.model = "";
        this.colour = "";
        this.registrationNumber = 0;


    }


    public Vehicle(String make, String model, String colour,
            int registrationNumber) 
    {
        this.make = make;
        this.model = model;
        this.colour = colour;
        this.registrationNumber = registrationNumber;
    }


    public String getMake() 
    {
        return make;
    }


    public void setMake(String make) 
    {
        this.make = make;
    }


    public String getModel() 
    {
        return model;
    }


    public void setModel(String model) 
    {
        this.model = model;
    }


    public String getColour() 
    {
        return colour;
    }


    public void setColour(String colour) 
    {
        this.colour = colour;
    }


    public int getRegistrationNumber() 
    {
        return registrationNumber;
    }


    public void setRegistrationNumber(int registrationNumber) 
    {
        this.registrationNumber = registrationNumber;
    }



    public String toString() 
    {
        return "Vehicle [make=" + make + ", model=" + model + ", colour="
                + colour + ", registrationNumber=" + registrationNumber + "]";
    }






}

public class Car extends Vehicle
{
private int doors;
private String shape;

public Car()
{
    super();
    this.doors = 0;
    this.shape = "";
}

public Car(String make, String model, String colour, int registrationNumber) 
{
    super(make, model, colour, registrationNumber);
    this.make = make;
    this.model = model;
    this.colour = colour;
    this.registrationNumber = registrationNumber;


}






}

The error message:

Description Resource    Path    Location    Type
The field Vehicle.make is not visible   Car.java    /VehicleApp/src line 19 Java Problem
The field Vehicle.model is not visible  Car.java    /VehicleApp/src line 20 Java Problem
The field Vehicle.colour is not visible Car.java    /VehicleApp/src line 21 Java Problem
The field Vehicle.registrationNumber is not visible Car.java    /VehicleApp/src line 22 Java Problem

Upvotes: 1

Views: 2926

Answers (5)

Corey Ogburn
Corey Ogburn

Reputation: 24769

private variables won't be visible. They're limited to just the class they're defined in. protected variables (and functions) will be visible to subclasses.

Change:

private String make, model, colour;
private int registrationNumber;

to

protected String make, model, colour;
protected int registrationNumber;

I recommend against keeping them private because that means that the Car does not inherit those variables from Vehicle. Forcing a Car to call it's own parent's getters and setters is introducing a gap between Car and Vehicle. It comes off as saying that a Car does not have colour, make, model, or a registration number, but it has to go to a separate class (Vehicle) and ask the class for it's colour, make, model, and registration number.

Upvotes: 5

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285440

You don't need to and in fact shouldn't set parent fields when you use the super constructor since the super(...) constructor call will do this for you.

public Car(String make, String model, String colour, int registrationNumber) 
{
    super(make, model, colour, registrationNumber);

    // get rid of these:
    // this.make = make;
    // this.model = model;
    // this.colour = colour;
    // this.registrationNumber = registrationNumber;
}

Upvotes: 2

Heskja
Heskja

Reputation: 785

Variables:

private means the variable is only visible within that class. For example

Class foo {
    private Object a; //a can only be accessed within foo.
}

protected means for that class and classes that intherit. For example

Class foo {
    protected Object a;
}

Class bar extends foo {
    public bar () {
        a = someVariable;
    }
}

public means all classes that can access a object of that class. For example

Class foo{
    public Object a;
}

Class bar{
    public bar(){
        foo foo = new foo();
        foo.a = someVariable;
    }
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503729

The "problem" is that the variables are private, so aren't available to the subclass. Presumably you're trying to access them directly within Car (it's hard to tell as you didn't include the source).

You could make them protected variables - but I would strongly advise you to leave them as private, and instead use the properties (the get and set methods) from your subclass. Fields are an implementation detail - you should consider what API you expose to subclasses as well as to other code.

For more details on access control in Java, see the JLS section 6.6. For example:

Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

Upvotes: 7

Brian Agnew
Brian Agnew

Reputation: 272417

Your Vehicle members are private. Consequently they're not visible to anyone except the Vehicle class. See here for more details.

Upvotes: 2

Related Questions