• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism - Java Homework Question  RSS feed

 
Anthony Stillwell
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question - Polymorphism comes into play only when there is inheritance (is –a) relationship between 2 classes. True or false? Explain.

My Answer: ( I am hung up on the word only)
True? Is this a trick question only when?
Remember, any Java object that can pass more than one IS-A test can be considered polymorphic. Other than objects of type Object, all Java objects are polymorphic in that they pass the IS-A test for their own type and for class Object.
According to textbook, Java How To Program – “Polymorphism enables you to “program in the general” rather than “program in the specific.” In particular, polymorphism enables you to write programs that process objects that share the same superclass (either directly or indirectly) as if they’re all objects of the superclass; this can simplify programming.” (pg 942)
With polymorphism, you can design and implement systems that are easily extended. New classes can be added with little or no modification to the general portions of the program, as long as the new classes are part of the inheritance hierarchy that the program processes. The only part of the program that must be altered for the new classes are those that require direct knowledge of the new classes that the programmer adds to the hierarchy.

Appreciate any opininons on the question. Thank you.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There certainly are other kinds of polymorphism in Java besides just the IS-A relationship. However, I have seen debates about what is and isn't polymorphism, so you your instructor may have a specific definition in mind, and there's no way anybody here can know what that is. Fortunately, you have the "Explain" part, so as long as you can articulate some reasonably defensible thoughts, and as long as your instructor is somewhat reasonable, you should get at least partial credit.
 
v thai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently, using interface can also achieve polymorphism, having several class to implement a interface, the concrete method to be invoke will be determine at runtime. So maybe the main point should be "program for interface", We should abstract the behavior and characteristic to some common template.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
v thai wrote:Apparently, using interface can also achieve polymorphism


That's inheritance, an IS-A relationship, which the OP mentioned as the one thing he's sure is polypmorphism, so it's not really an "also".

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Stillwell wrote:My Answer: ( I am hung up on the word only)...

I think you may be 'overthinking' this; and when you're writing an answer you need to be absolutely certain that you're not confusing an object with a type.

An object is not intrinsically polymorphic. It's type, however, may be. If I create an ArrayList:
private ArrayList<X> myArrayList = new ArrayList<X>();
I cannot use 'myArrayList' as anything other than an ArrayList; however, I can pass it to a method that is defined to take a List, because ArrayList (the type) is defined as implementing the List interface.

I hope you see the difference. Polymorphism is context- (ie. structure-) driven; objects are not. They are simply objects; of the type they were defined as. If ArrayList didn't implement List, then myArrayList would be no more polymorphic than...well...me (and believe me, I'm not).

HIH

Winston
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony, your answer is excessively wordy and doesn't really address the question. The question doesn't ask what polymorphism enables you to do; it doesn't ask whether polymorphism is fabulous or terrible. Focus on the assertion in the question and either support or refute it. If you think it's false, provide a counter-example. If I were grading this question that's what I'd be looking for.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:Your answer is excessively wordy and doesn't really address the question.

And it wasn't meant to. We (I assume) know the answer to the question, and it will be either 'true' or 'false'; but what that provides to someone who is new is questionable, to say the least. As it says at the top of the Thread: "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers".

Winston
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is an important phrease in the question: Polymorphism comes into play he isn't asking for a discussion on polymorphism, but when it happens
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendy Gibbons wrote:I think this is an important phrease in the question: Polymorphism comes into play he isn't asking for a discussion on polymorphism, but when it happens

Fair enough, but from the answer given, I'm wasn't sure that Anthony is sure of precisely what polymorphism is; hence wordy answer.

Winston
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Stillwell wrote:Question - Polymorphism comes into play only when there is inheritance (is –a) relationship between 2 classes. True or false? Explain.

My Answer: ( I am hung up on the word only)
True? Is this a trick question only when?
Remember, any Java object that can pass more than one IS-A test can be considered polymorphic. Other than objects of type Object, all Java objects are polymorphic in that they pass the IS-A test for their own type and for class Object.
According to textbook, Java How To Program – “Polymorphism enables you to “program in the general” rather than “program in the specific.” In particular, polymorphism enables you to write programs that process objects that share the same superclass (either directly or indirectly) as if they’re all objects of the superclass; this can simplify programming.” (pg 942)
With polymorphism, you can design and implement systems that are easily extended. New classes can be added with little or no modification to the general portions of the program, as long as the new classes are part of the inheritance hierarchy that the program processes. The only part of the program that must be altered for the new classes are those that require direct knowledge of the new classes that the programmer adds to the hierarchy.


Appreciate any opininons on the question. Thank you.


Is the bit in bold your answer or part of the question?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendy Gibbons wrote:I think this is an important phrease in the question...

I have removed the "(and, in general, a class)" from my original post, because in this context it was wrong. Thanks.

Winston
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Dennis Deems wrote:Your answer is excessively wordy and doesn't really address the question.

And it wasn't meant to. We (I assume) know the answer to the question, and it will be either 'true' or 'false'; but what that provides to someone who is new is questionable, to say the least. As it says at the top of the Thread: "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers".

Winston

Sorry for the confusion, my post was addressing the original poster. He asked us to critique his answer to a question.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:Sorry for the confusion, my post was addressing the original poster. He asked us to critique his answer to a question.

In which case, my apologies too.

I have to admit, I wish it was a bit easier to determine that sort of stuff here. Since JR seems to be icon-happy, a 'replied to' might be appropriate.

Winston
 
Anthony Stillwell
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone for input. I will let you know how the instructor replies to my answer once I get the homework graded. I have to say you made me think, I am still not sure if the answer is True or False based on the wording of the question but I have a much better understanding of Polymorphism.

This was the Question from the class - Polymorphism comes into play only when there is inheritance (is –a) relationship between 2 classes. True or false? Explain.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Stillwell wrote:This was the Question from the class - Polymorphism comes into play only when there is inheritance (is –a) relationship between 2 classes. True or false? Explain.

It's been a while since I did exams. but as I recall (and as Dennis said), the object is to choose a side and explain your reasons. Most of the people who've replied already know the answer. Your task, as they say in the courts, is to prove it...or at the very least, argue a good case for it.

Good luck.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!