Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-10-2006, 04:47 PM   PM User | #1
Cat22
New Coder

 
Join Date: Mar 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Cat22 is an unknown quantity at this point
Java Inheritance question

Hi, i have following code that works OK:

public class Building {

protected String name;

A(String name){
this.name = name;
}
public void printSpecs() {}
}

and two inherited classes :


public class House extends Building {
protected double maxDimension;
House (String name, double maxDimension) {
super(name);
this.maxDimension= maxDimension;
}

public void printSpecs()
{
System.out.println(maxDimension);
}
}

public class Castle extends Building {

protected double maxWeight;

Castle (String name, double maxWeight) {
super(name);
this.maxDimension= maxWeight;
}

public void printSpecs()
{
System.out.println(maxWeight);
}
}

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
Cat22 is offline   Reply With Quote
Old 08-10-2006, 05:01 PM   PM User | #2
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
The reason it asks for a return type is because you can't have an empty method with a return type of int.

int GetSpecs() {}

You could only do this if the class was abstract (if I remember right). but if it was abstract then you would want to impliment it in the child classes.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 08-10-2006, 05:08 PM   PM User | #3
Cat22
New Coder

 
Join Date: Mar 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Cat22 is an unknown quantity at this point
ok,

I will definitely implement getSpecs() {} in child classes, but what should i write in the main class?
Cat22 is offline   Reply With Quote
Old 08-15-2006, 02:02 PM   PM User | #4
Jarl
New to the CF scene

 
Join Date: Aug 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Jarl is an unknown quantity at this point
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.

Hope that helps.

Last edited by Jarl; 08-15-2006 at 02:05 PM..
Jarl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:56 PM.


Advertisement
Log in to turn off these ads.