1

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
3
  • Please post the actual error message, not a paraphrasing of the message, and indicate with comments the lines that are causing the error. Don't make us guess at this information. Plus post the child classes. Your question will get answers this time, but in the future incomplete questions may not. Commented Oct 19, 2012 at 16:51
  • 1
    inheritance works, even stuns! your code doesn't work. edit your post please. Commented Oct 19, 2012 at 16:51
  • 1
    post your sub class Car.java Commented Oct 19, 2012 at 16:52

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

11 Comments

I have getters and setters in the parent. Im obviously missing something because i always use private when initialising variables in the parent and child classes.
@Pendo: again show the child classes and the actual error messages. Else we won't know what you're missing.
@Pendo826: You've got getters and setters, yes - but presumably you were trying to access the variables directly from Car, which you can't do - you have to call the getters/setters. It's very hard to show you what you're doing wrong when you haven't shown us the code that doesn't compile...
I preferred your other answer. Not that I don't agree with the above
I edited the Car class constructor. On the lines this.make = make; this.model = model; this.colour = colour; this.registrationNumber = registrationNumber; These are showing the error.
|
5

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.

1 Comment

To the original poster: No, don't do this. Jon Skeet has the better answer. You want to limit visibility whenever and wherever possible.
2

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

6 Comments

Dammit. How did i overlook this. I am obviously a bit rusty. Thanks Hovercraft.
@Pendo: no problem, that's what we're here for.
Thanks for the "accepting" my answer, but in fact @JonSkeet's answer is the correct one. My answer is just an addendum to his.
Technically you helped me find the solution. But if you would like me to accept his. I will. And thanks again i appreciate it.
@Pend: It probably doesn't matter since neither he nor I really need the points.
|
2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.