We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Inheritance Problem
Page Index Toggle Pages: 1
Inheritance Problem (Read 1453 times)
Inheritance Problem
May 29th, 2010, 9:03pm
 
Hi, I was wondering if someone might be able to explain this to me.

I have a 'Content' Class which has two sub classes: 'ContentImage', 'ContentMovie'.

I have an array of type 'Content' which contains both types of the sub classes.

Both the sub classes have the method 'getContent', which returns either an Image or a Movie. However, when I run the program it doesn't recognise the 'getContent' method.

I think I'm going wrong when I'm getting the 'Content' object from the array and trying to use a sub class method. If this is the case, how would I tell it which type it is??

In advance. Thank you for the help



Re: Inheritance Problem
Reply #1 - May 30th, 2010, 12:53am
 
It is hard to answer without seeing some code structure...
But well, from your description, I think Content must be abstract, ie. you probably will never do something like new Content();
And it must define a method, perhaps abstract itself (without code), getContent(). This forces the sub-classes to implement this method, and tells the compiler it can be sure any class of type Content will have this method.

Perhaps this is what you did already, I don't know.
If needed, show your code (you can remove the code in the methods if it is long).
Re: Inheritance Problem
Reply #2 - May 30th, 2010, 1:20pm
 
I wonder also: You say that the ContentImage class returns an image from its getContent() method, while the ConentMovie class returns a movie.  Do they return different data types then?  It sounds like it.  If so, they'll be considered two different methods and you'll need to know which subclass you're accessing.

So, there are two or three issues you might be facing; if you could post a little code it would help.

Page Index Toggle Pages: 1