Tim Carew
Tim Carew

Reputation: 31

How to access methods in subclasses from main method? (Java)

I am learning about superclasses and subclasses. I have to create a superclass (Campsite) and then two subclasses(FrontCountry and BackCountry), one of which has two additional subclasses(Serviced and Unserviced). Not bad so far, I can do all of that. But i'm asked this question at the very end.

*In CampTester, write another static Java method called statistics which takes, as its parameter, an array of Campsite objects and determines and outputs the following: the attribute values for each object
the total number of Front Country sites in Terra Nova National Park, Newfoundland and Labrador
the total number of serviced sites (with 30 amp service) in Newman Sound, Terra Nova National Park, Newfoundland and Labrador
the locations of all Back Country sites in Gros Morne National Park, Newfoundland and Labrador
a list, by site number, of all the unserviced sites in Malady Head, Terra Nova National Park, Newfoundland and Labrador

I'm trying to use an enhanced for loop to print the attributes for each object in an array that i've created. simple enough. The problem lies within the fact that I cannot access the methods that can retrieve the information from the subclasses that I need to tally and present.(methods like getLocation, getAmpService, get Province)

IN ESSENCE:

Is it possible to access methods defined in a subclass from outside of that subclass (higher in the hierarchy)?

Upvotes: 0

Views: 838

Answers (4)

Weidian Huang
Weidian Huang

Reputation: 2945

Not sure I understand your issue correctly, it seems you didn't do the casting when you access the methods in subclass.

Assume that getLocation, getAmpService methods are only existing in FrontCountry, but not in superclass Campsite. Before you want to access these methods, you need to cast the object into FrontCountry.

if (campsite instanceof FrontCountry) {
     FrontCountry frontCountry = (FrontCountry)campsite;
     frontCountry.getLocation();
     frontCountry.getAmpService();
}

Upvotes: 0

Sondering Narcissist
Sondering Narcissist

Reputation: 419

JLaudio's answer is definitely correct. I'd also like to add that superclasses cannot access subclass methods (as your shortened question might be suggesting). The reason for this is that the class structure allows subclasses to be more specialized than their parents. So a method that is defined specifically in a subclass (FrontCountry or BackCountry) would not be available for a Campsite object to call.

Upvotes: 0

M1LKYW4Y
M1LKYW4Y

Reputation: 598

This would be easier to answer with the code you have so far, but basically if your FrontCountry class extends your Campsite class, and you have an array of campsites, you can do things like this to access the FrontCountry methods:

if (campSiteArray[0] instanceof FrontCountry){
  ((FrontCountry)campSiteArray[0]).frontCountryMethod();

Upvotes: 0

JLaudio
JLaudio

Reputation: 63

The class variable location should be defined inside the Campsite class and it should be protected. Protected class variables are inherited. The campsite class should also have public getLocation() and setLocation() methods. The sub classes will inherit location, getLocation() and setLocation(). Any instance of Campsite (including subclass instances) will have the camp.getLocation() method.

Upvotes: 1

Related Questions