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().

JavaScript

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:

JavaScript

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