[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Python is weak typed script language. It can support "Polymorphism without inheritance".
Java is a strong typed language. It is impossible to do "Polymorphism without inheritance".
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Python is weak typed script language. It can support "Polymorphism without inheritance".
James Clark wrote:
Jane, it would be helpful if you could explain what you mean by this. How does Python support the OO design concept of polymorphism without using inheritance of either a concrete class or an interface class.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
When you call a method on an object, the runtime environment checks whether that object has an implementation for that specific method. If so, it gets called polymorphically;
James Clark wrote:
When you call a method on an object, the runtime environment checks whether that object has an implementation for that specific method. If so, it gets called polymorphically;
What is "polymorphic" about this process?
A JRE does a similiar check at runtime as well.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
The method implementation used is determined at runtime, depending on the implementation of the object.
James Clark wrote:
The method implementation used is determined at runtime, depending on the implementation of the object.
An object only has a single implementation ... at all times. I don't see anything "polymorphic" in this description.
The method implementation to execute is always determined at run-time, it is never determined during compilation.
(Polymorphism compilation rules) and (Polymorphism run-time behavior) are different concepts, and handled by different code.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Kengkaj Sathianpantarit wrote:In Java we can implement polymorphism by using interface or inheritance.
So an answer of this question is using interface.
Or you mean you don't want to use both interface and inheritance?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss wrote:
Kengkaj Sathianpantarit wrote:In Java we can implement polymorphism by using interface or inheritance.
So an answer of this question is using interface.
Or you mean you don't want to use both interface and inheritance?
Interfaces are just a specialized form of inheritance.
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Kengkaj Sathianpantarit wrote:I don't think so. A class that *implements* an interface doesn't *inherit* anything.
David Newton wrote:
Kengkaj Sathianpantarit wrote:I don't think so. A class that *implements* an interface doesn't *inherit* anything.
A specialized form of inheritance, and I disagree: it inherits the interface definition--just not any behavior.
Interfaces are a replacement for multiple inheritance.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss wrote:The only way to do it in Java is to use reflection. And that's clumsy.
Kengkaj Sathianpantarit wrote:In Java we can implement polymorphism by using interface or inheritance.
So an answer of this question is using interface.
Or you mean you don't want to use both interface and inheritance?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
David Newton wrote:
Kengkaj Sathianpantarit wrote:I don't think so. A class that *implements* an interface doesn't *inherit* anything.
A specialized form of inheritance, and I disagree: it inherits the interface definition--just not any behavior.
Interfaces are a replacement for multiple inheritance.
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Jane Somerfield wrote:
Kengkaj Sathianpantarit wrote:In Java we can implement polymorphism by using interface or inheritance.
So an answer of this question is using interface.
Or you mean you don't want to use both interface and inheritance?
Java is a language supports multiple inheritance through Interfaces, not Classes.
2.2.5 No More Multiple Inheritance
Multiple inheritance--and all the problems it generates--was discarded from Java. The desirable features of multiple inheritance are provided by interfaces--conceptually similar to Objective C protocols.
Interfaces and Multiple Inheritance
Interfaces have another very important role in the Java programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. The Java programming language does not permit multiple inheritance (inheritance is discussed later in this lesson), but interfaces provide an alternative.
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Ilja Preuss wrote:strange - I don't think I've ever heard the the term interface class before. Google doesn't seem to know much about it either.
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jane Somerfield wrote:
Ilja Preuss wrote:The only way to do it in Java is to use reflection. And that's clumsy.
How?
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:
Jane Somerfield wrote:
Ilja Preuss wrote:The only way to do it in Java is to use reflection. And that's clumsy.
How?
Here's some code that's pretty much how you do it. (I typed this without a compiler so it might not be 100%. Google "reflection invoke method" for a full example. I was trying to focus on the steps/pseduocode.)
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Jeanne Boyarsky wrote:
Here's some code that's pretty much how you do it. (I typed this without a compiler so it might not be 100%. Google "reflection invoke method" for a full example. I was trying to focus on the steps/pseduocode.)
This isn't something you want to do for the purpose of inheritance though. For a basic construct like that, it's better to use what the language supports - interfaces. That way when people read the code, it uses the standard Java idioms.
Reflection does have a use - when you are writing utilities or don't have the ability to force interfaces on the objects they deal with.
Jane Somerfield wrote:
Jeanne Boyarsky wrote:
Here's some code that's pretty much how you do it. (I typed this without a compiler so it might not be 100%. Google "reflection invoke method" for a full example. I was trying to focus on the steps/pseduocode.)
This isn't something you want to do for the purpose of inheritance though. For a basic construct like that, it's better to use what the language supports - interfaces. That way when people read the code, it uses the standard Java idioms.
Reflection does have a use - when you are writing utilities or don't have the ability to force interfaces on the objects they deal with.
It is not polymorphism. Your containing class consists of a class of clazz. So the behave of clazz is part of the containing class.
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Jane Somerfield wrote:
It is not polymorphism. Your containing class consists of a class of clazz. So the behave of clazz is part of the containing class.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Ilja Preuss wrote:
Jeanne's code is in fact exactly emulating how polymorphism is implemented in dynamically typed languages.
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Kengkaj Sathianpantarit wrote:If not using inheritance just means not using a superclass that define common methods, this solution is all right.
At least we have to use java.lang.Object because it's a language constraint, but we don't use it for polymorphism purpose.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Don't get me started about those stupid light bulbs. |