• Post Reply Bookmark Topic Watch Topic
  • New Topic

polymorphism/inheritance question  RSS feed

 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just when I grasped the concepts of extend and implements(interfaces), then comes along abstract. I cannot find anything that describes all three in the same context as inheritance and/or polymorphism.

Beginning to think I need someone to draw me a picture! Here's a "real life" I have from videos I've been watching:

Primates: Superclass
Humans: Subclass

Then you have a class of Dolphins
and lastly an Intelligent class.

Primates extends Humans
Dolphin is not a Primate but shares the Intelligent characteristic of Humans and thus: Dolphins can also implement Intelligent

Now my question is: Where in the heck does an abstract class and perhaps an abstract method come into play in relation to this concept of of Inheritance and Polymorphism?

Can someone provide an example that goes along with mine that would explain the purpose of abstract?

 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an advanced question and shouldn't be in Beginning Java forum.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Primates is a primary one...And not only human is a primate... Lemurs, Apes are also in Primates category...

Humans, Lemurs, Apes have common behavior and does it in different way...

So, Primates will become an abstract class containing the general behaviors( abstract methods)... Since defining methods in Primates does not make sense it has been made as abstract...

Abstract class gives the pattern of abstract methods to be followed by its sub-classes in its own way... So there comes Polymorphism (Method Overriding)...

Human, Apes class can define the behavior in their own way...

So, Primates can be anything... It can be Ape, Human, or Lemurs... So one entity can be of multiple forms... This is Polymorphism...



Now, coming to Dolphin... It is of different set...

Both Human and Dolphin matches in Intelligent category...

So, in JAVA aspect, abstract class cannot be used for both Human and Dolphins due to Multiple Inheritance issue,

So, Interfaces are used to declare the Intelligent related methods which is itself abstract by default...

Now, "Intelligence" interface can be implemented by both Human and Dolphin...

And according to Polymorphism concept, Intelligence can be possessed by human or dolphin... can be of any form...

 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread will be even more fine to get the explanation of Polymorphism/Inheritance

http://www.coderanch.com/t/523445/java/java/Polymorphism-example
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my mind, they all let you do the same thing. You can think of an abstract class as halfway between an interface and a full blown class. There are one or more methods that are not implemented, and zero or more that ARE implemented (I think that's right).

But remember, you can only extend ONE class - be it a concrete or a abstract. You can implement as many interfaces as you want/need.

I assume in your original post, you meant that it should be "interface Intellegent". Also, if primates are the superclass, then "class Humans extends Primates", not the other way around (as you wrote it).

So if i have

public class Human extends primate implements Intellegent {}
public class Dolphin implements Intellegent {}

I can later do this:

Intelligent smartCreature = new Human();
or
Intelligent smartCreature = new Dolphin();

further, I can create a collection of "Intelligent" creatures. they can be Humans, Dolphins, Pigs, Borg, Klingon, or Cyborgs - as long as they implement the Intelligent interface.


 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Thanks for all the info. I especially appreciate the use of syntax in explaining this and it does read correctly in that the subclass extends the superclass....my brain is on overdrive. I've written HTML and CSS for years and that's how I've always learned..by reading it like an ordered instruction manual. I've wanted to learn Java since 1997 and have finally found resources that actually help (including here).

This is an advanced question and shouldn't be in Beginning Java forum.

My apologies if this is more "advanced" for this forum...interfaces and inheritance however are in fact covered in every beginning Java video/tutorial/class, etc.

And Fred, thanks for
You can think of an abstract class as halfway between an interface and a full blown class.

That is just what I needed..to put it in perspective.

Back to it...Have a good one and many thanks again!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jennifer Schwartz wrote:My apologies if this is more "advanced" for this forum...interfaces and inheritance however are in fact covered in every beginning Java video/tutorial/class, etc.

Don't worry about it. Ofcourse we do appreciate it that you think about what forum you're going to post your question in. If one of the moderators thinks that a question belongs in another forum, he or she will move it there for you.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jennifer Schwartz wrote: . . . My apologies if this is more "advanced" for this forum...interfaces and inheritance however are in fact covered in every beginning Java video/tutorial/class, etc. . . .
Well, I think interfaces are a "beginning" topic, so it isn't too advanced.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an example for polymorphism through interface:



What is advantage of using interface vs inheritance for polymorphism?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can extend only one class, and implement any number of interfaces you want.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Polymorphism and interface. Does this example show usage of interface for polymorphism well?Any suggestion?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is polymorphism because it is multiforms of raise methods. Right?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your example does show polymorphism, yes.
But you have poor style, using small letters and _ characters. You are writing Java™, not C
It also has poor design, using static constants rather than passing the percentages as arguments.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!