Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Polymorphysm

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following class inheritance scenarios describes polymorphism in Java?
1 one class inherits from multiple parent classes
2 one class implementing many interfaces
3 multiple classes implementing one interface
4 multiple classes inherit from single abstract parent class

I saw different answers in different places. Is anybody 100% about the answers?
 
Rajan Choudhary
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel, 3 & 4 should be the correct ones but somehow I remember I saw 1 & 2 correct ones...
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, 1's not even possible in Java, so you can rule that out.

Edit: unless they mean an inheritance chain, I suppose.

I'd agree, 3 and 4 are the best fit. Polymorphism is about treating objects as if they are of a common class, and they behave appropriately according to which subclass they are an instance of. You could make a case that polymorphism can happen whenever you have inheritance, but 3 and 4 are a better example for demonstrating it.
 
Rajan Choudhary
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I too thought about chained inheritance. You and I are in favor of 3 & 4. I would go by that unless someone comes up with good clarification. Thanks for your comments.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say the best answer that applies here is 3.
Inheriting from an abstract class does not guarantee that a method can change or will change its behavior in subclasses
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
POLY-morphism means that two classes have the same interface, but different behavior. Thus, options 1 and 2 are out.

Option 4 says nothing about common interface of these multiple classes. "Abstract class" doesn't mean that it has any methods in it.

Thus, the only option is 3.
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic