Question

I´m getting cannot find symbol error in my code (symbol: method setAr(boolean)).

Here is my Main.java file:

class Vehicle {
    protected int marchs;
    protected int rode;
    public void xydar(int km) { System.out.print("\nxydei "+ km +" km!"); }
}
class Car extends Vehicle {
    public Car() { this.rode = 4; }
    public void xydar(int km) {
    super.xydar(km);
    System.out.println(" Estou de car!");
    }
}
class CarLux extends Car {
    private boolean ar;
    public CarLux() { this.ar = true; }
    public void setAr(boolean newAr) { this.ar = newAr; }
    public void xydar(int km) {
        super.xydar(km);
        if (this.ar)
        System.out.println(" ON!");
        else System.out.println(" OFF!");
    }
}
public class Main {
public static void main(String []args) {
    Vehicle moto = new Vehicle();
    moto.xydar(90);
    Vehicle car1 = new Car();
    car1.xydar(100);
    Vehicle car2 = new CarLux();
    car2.xydar(400);
    car2.setAr(false);
    car2.xydar(400);
    }
}

How can I call setAr() method correctly? Can anyone help me? I´m new to Java. Thanks in advance.

Was it helpful?

Solution

You need to declare car2 as a CarLux, not a Vehicle.

CarLux car2 = new CarLux();

That's because your setAr() method is defined on CarLux. car2 is currently held in a variable of type Vehicle, so when you call a method of car2 only the methods declared by Vehicle will be available.

OTHER TIPS

You can call setAr method only through an object of type CarLux because this is method of CarLux not Vehicle so you have to cast car2 as CarLux then call method like this -

((CarLux)car2).setAr(false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top