Skip to content
Advertisement

Accessing objects in other class Java inheritance

In my program I have class Vehicle and class Car which inherit from Vehicle. I’ve created two Car’s objects c and c2. Now I have to make sumFuel() method in Calculate class which sums fuel used by Car’s objects.

c.fuel+ c2.fuel; It works when I write it in main, but how can I do this in class method? I’m also considering doing array of Car’s objects, but I don’t know where I should place it and how to refer to it in sumFuel().

package javaapplication25;


public class JavaAplication25 {

    public static void main(String[] args) {
       
       Car c= new Car();
       Car c2= new Car();
       
       c.setVehicle(200,5547,50);
       c.display();
       
       c2.setVehicle(150,5087,100);
       c2.display();
        
        
        
    }
    
}
class Vehicle
{
    int speed;
    int nr;
   
    void setVehicle(int speed, int nr)
    {
        this.speed=speed;
        this.nr=nr;
    }
    void display()
    {
         System.out.println("Speed: "+speed );
         System.out.println("Nr: "+nr);
         
    }

}
class Car extends Vehicle
{
   int fuel;
   void setVehicle(int speed, int nr, int fuel)
   {
          
  
         super.setVehicle(speed, nr);
         this.fuel=fuel;
         
   }
   void display()
   {
       super.display();
       System.out.println("Fuel: "+ fuel);
   }
   
}
class Calculate extends Car
{
   int sum=0; 
   /*int sumFuel()
   {
       
      
   }*/
}

Advertisement

Answer

The code snippet says you are a novice in java: Try to understand the uses of private, public and protected access modifiers and how to use constructors to instantiate the object with some data.

Coming back to your question just try this:

public class JavaAplication25 {

public static void main(String[] args) {

    Car c = new Car(200,5547,50);
    Car c2 = new Car(150,5087,100);

    c.display();
    c2.display();

    Car cars[] = {c,c2}; //array of cars
    Calculate calculateFuel = new Calculate();
    System.out.println("Total fuel:" + calculateFuel.sumFuel(cars));
}

private static class Vehicle {
    private int speed;
    private int nr;

    Vehicle(int speed, int nr) {
        this.speed=speed;
        this.nr=nr;
    }
    protected void display() {
        System.out.println("Speed: "+speed );
        System.out.println("Nr: "+nr);
    }
}

private static class Car extends Vehicle {
    private int fuel;

    Car(int speed, int nr, int fuel) {
        super(speed, nr);
        this.fuel=fuel;
    }

    protected void display() {
        super.display();
        System.out.println("Fuel: "+ fuel);
    }

}

private static class Calculate {
    private int sum = 0;

    private int sumFuel(Car arrayOfCars[]) {
        for (int i=0; i<arrayOfCars.length; i++) {
            sum = sum + arrayOfCars[i].fuel;
        }
        return sum;
    }
}
}

Don’t just try to get the solution to this question but also try to understand the access modifiers and constructors.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement