The Artima Developer Community
Sponsored Link

Java Answers Forum
the inheritance and polymorphism

5 replies on 1 page. Most recent reply: Dec 4, 2002 5:50 PM by Michael Will

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Kevin

Posts: 2
Nickname: lymix
Registered: Aug, 2002

the inheritance and polymorphism Posted: Aug 6, 2002 11:15 PM
Reply to this message Reply
Advertisement
Could I implement the polymorphism without the inheritance in java?Someone here said that's not impossible mission,but i just can't understand.Anyone could help me?Thanks!


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: the inheritance and polymorphism Posted: Aug 7, 2002 3:09 AM
Reply to this message Reply
They were probably talking about using composition versus inheritance.

What specific problem are you trying to solve?

Kevin

Posts: 2
Nickname: lymix
Registered: Aug, 2002

Re: the inheritance and polymorphism Posted: Aug 7, 2002 3:52 AM
Reply to this message Reply
The question is not from any specific problem.It's just a theory puzzle.
I know the difference between the compisition and inheritance,but it's not the question of that.They(some training men)addressed that they could implement the polymorphism without inheritance,and later they said if we were interested in that,we could take their training courses.I think it's impossible in Java at least.Maybe that's true in C++ with the help of STL or other oo language.But I don't know about that.Anyway,thank you for your replying!

Don Hill

Posts: 70
Nickname: ssswdon
Registered: Jul, 2002

Re: the inheritance and polymorphism Posted: Aug 7, 2002 6:11 AM
Reply to this message Reply
Are we not doing this by implementing a Interface and then coding to the implementation ?

Michael Will

Posts: 3
Nickname: zmichael
Registered: Dec, 2002

Re: the inheritance and polymorphism Posted: Dec 4, 2002 5:43 PM
Reply to this message Reply
> Are we not doing this by implementing a Interface and then
> coding to the implementation ?

Inheritance of behaviour means inheriting interface AND implementation.

In java, you can only directly inherit from one baseclass, but you can implement multiple interfaces. So whenever you face a situation where you want to inherit the implementation too, you are forced to duplicate the code for it.

illegal: class c extends a,b
just use methods of a and b
class d extends a, b
(and d should not extend c when it has no is-a relationship)

legal: class c implements ia, ib
+code for methods of a and b
then use that implementation.
class d implements ia, ib
there we go again. or we extend c but
what if they have no real is-a relation?

Or you do create an extra "mixin" class that implements both interfaces, which you can then extend, practically extending both at once.

class mixin implements ia, ib
class c extends mixin
class d extends mixin

I think that is how I would do it.

Michael

Michael Will

Posts: 3
Nickname: zmichael
Registered: Dec, 2002

Re: the inheritance and polymorphism Posted: Dec 4, 2002 5:50 PM
Reply to this message Reply
> Are we not doing this by implementing a Interface and then
> coding to the implementation ?

Inheritance of behaviour means inheriting interface AND implementation.

In java, you can only directly inherit from one baseclass, but you can implement multiple interfaces. So whenever you face a situation where you want to inherit the implementation too, you are forced to duplicate the code for it.

Assume there are two classes a,b which implement something
unrelated each. now assume you want to implement classes c and d who also implement something unrelated, but both want to do what a and what b does without reimplementing the wheel.

illegal:
- class c extends a,b
- just use methods of a and b
- class d extends a, b
- d should not extend c when it has no is-a relationship

legal:
- class c implements ia, ib
- +code for methods of a and b
- then use that implementation.
- class d implements ia, ib
- +code for methods a and b duplicated
- or we extend c but what if they have no real is-a relation?

Or you do create an extra "mixin" class that implements both interfaces, which you can then extend, practically extending both at once.

- class mixin implements ia, ib
- class c extends mixin
- class d extends mixin

I think that is how I would do it. Because you model "what a and what b does" as "mixin" and then extend that.

Michael

Flat View: This topic has 5 replies on 1 page
Topic: Applet destroying Previous Topic   Next Topic Topic: try/catch help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use