-
Interface
Hi,
I have question for you relating Java programming. The problem is multiple
inheritance. I have a class as follows:
public Class MapObject {
MapObject next;
MapObject getnextObject(MapObject Obj){
....
}
setnextObject(MapObject Obj){
....
}
}
Now I want to have a class Rectangle which should inherit from the class
MapObject and also from another class Container.
How can I do this. If I use an interface, will I be able to do this. As I
dont know that we can define a Class variable like next in the example given
in an interface. Please write a snippet if possible to elaborate the example.
Please help me out.
Thanks.
Saad.
-
Re: Interface
Do you have code in your MapObject class? If you do, then you can't define
it as an interface. But if you expect Rectangle to implement the methods,
then define MapObject as an interface.
When you say you want Rectangle to inherit from both Container and from
MapObject, you have to decide which of these is truer. It's only true
inheritance if you expect to assign your Rectangle object (at different
times) to variables of type Container and of type MapObject. If you don't
expect to do this assignment, then you can use delegation instead of
inheritance. Here's how you would do that, assuming that Rectangle only
needs to use the functionality of MapObject:
public class Rectangle extends Container {
private MapObject delegate;
private float length, width;
public Rectangle(float newLength, float newWidth) { // constructor
length = newLength;
width = newWidth;
delegate = new MapObject();
}
public MapObject getMapper() { // to implement MapObject
return delegate;
}
// other methods...
}
Now instead of using "MapObject newObj = myRectangle.getNextObject()", you
can use "MapObject newObj = myRectangle.getMapper().getNextObject()".
On the other hand, if you expect Rectangle to implement the getNextObject()
and other methods, then use an interface:
public interface MapObject {
public MapObject getNextObject();
// etc.
}
and have Rectangle implement it:
public class Rectangle extends Container implements MapObject {
// variables...
// constructor...
public MapObject getNextObject() {
// code...
return something;
}
}
PC2
ijad <smihs@hotmail.com> wrote in message news:39d61a46$1@news.devx.com...
>
> Hi,
>
> I have question for you relating Java programming. The problem is multiple
> inheritance. I have a class as follows:
>
> public Class MapObject {
>
> MapObject next;
>
> MapObject getnextObject(MapObject Obj){
> ....
> }
>
> setnextObject(MapObject Obj){
> ....
> }
>
> }
>
> Now I want to have a class Rectangle which should inherit from the class
> MapObject and also from another class Container.
>
> How can I do this. If I use an interface, will I be able to do this. As I
> dont know that we can define a Class variable like next in the example
given
> in an interface. Please write a snippet if possible to elaborate the
example.
>
>
> Please help me out.
>
> Thanks.
>
> Saad.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks