Search code examples
javaclassinheritanceoverridingsuperclass

How do I override the variable that was given a value to in another class which has been inherited


I am trying to get user input for the number of seats a car has in the super class but make a method that prints the number of seats in the subclass called "Car". However, when I state the variable that stores the user input I get an error stating the variable is not visible and that is because it is in another class.

import java.util.Scanner;

class Vehicle {

    Scanner s = new Scanner(System.in);
    private String color;
    private int noOfCylinders;
    private int noOfSeats;

    public Vehicle() {
        color = "Black";
        noOfCylinders = 0;
        noOfSeats = 1;
    }

    public Vehicle(String color, int noOfCylinders, int noOfSeats) {
        this.color = color;
        this.noOfCylinders = noOfCylinders;
        this.noOfSeats = noOfSeats;
    }

    public void getColor() {
        System.out.print("Enter color of vehicle: ");
        color = s.nextLine();
    }

    public String setColor() {
        return color;
    }

    public void getNoOfCylinders() {
        System.out.print("Enter number of cylinders: ");
        noOfCylinders = s.nextInt();
    }

    public int setNoOfCylinders() {
        return noOfCylinders;
    }

    public void getNoOfSeats() {
        System.out.print("Enter numer of seats: ");
        int noOfSeats = s.nextInt();
    }

    public String toString() {
        String information;
        information = "is " + color + " and it has " + noOfCylinders + " cylinders.";
        return information;
    }
}

public class CreateVehicle {
    public static void main(String[] args) {

        Car CarObject = new Car();
        Truck TruckObject = new Truck();

        CarObject.getColor();
        CarObject.setColor();
        CarObject.getNoOfCylinders();
        CarObject.setNoOfCylinders();
        CarObject.toString();
        CarObject.getNumOfSeats();

        TruckObject.getColor();
        TruckObject.setColor();
        TruckObject.getNoOfCylinders();
        TruckObject.setNoOfCylinders();
        TruckObject.toString();

        System.out.print(("\nThe car ")+CarObject.toString());
        System.out.print(("\nThe truck ")+TruckObject.toString());      
    }
}


class Car extends Vehicle{

    public void getNumOfSeats(){
        System.out.print("\nThe car has " + noOfSeats + " seats.");
    }
}


    class Truck extends Vehicle {
        public void printTowingCapacity() {
            System.out.print("\nThe car has " + towingCapacity + ".");
        }
    }

Solution

  • Based on OOP concept you can't access private variabale outside the class not even in child class.

    Here you have declare

     private int noOfSeats;
    

    And you try to access it outside the class in child which is out of private variable scope.

    To access it in child class make noOfSeats variable

     public int noOfSeats;
    

    or

     protected int noOfSeats;
    

    in Vehicle class.

    If you want to put noOfSeats variable as private the create on public function on vehicitle class (parent class) and call it from Car class (child class).

    And you are try to access

     System.out.print("\nThe car has " + towingCapacity + ".")
    

    towingCapacity variable in Truck class and you have't declare it in either Vehicle class or Truck class.

    So first declare it then use it.

    For more details about java access modifier read this article

    https://www.softwaretestingmaterial.com/access-modifiers-in-java/