If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

i have array of objects
HashMap <int, Building > builds = new HashMap<int, Building >();
and some of them are of type "House" and some "Castle"

when i do following:
Building myBuild = builds .get(x);
myBuild.printSpecs();

everything works: specs are printed according to class type.

Now, the question: I want to add function

int GetSpecs() {}
to "father" class "Building" so that result will be returned by inherited class (similar to function printSpecs)
BUT, when i add such empty function i get error "function GetSpecs must return results".
So, what's the right syntax/way to do it ?
Thanks

My Java is a little rusty but as the previous poster mentioned you should just be able to define the superclass abstract:

public abstract class Building {
protected String name;

Building(String name) {
this.name = name;
}

And include your abstract method like so:

public abstract int getSpecs();

You will need to provide the implementation for this method in each subclass (or else also define the subclass as abstract) that inherits from Building though. Also you won’t be able to instantiate a Building object itself—which would stand to reason.