7 Replies - 1625 Views - Last Post: 16 April 2017 - 07:21 AM

#1 jeffindenver  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 114
  • Joined: 07-August 15

Polymorphism - I love it

Posted 22 October 2015 - 07:24 AM

I do. I love polymorphism.

Thanks to some good, constructive criticism here on DIC and elsewhere, I have the re-usability idea lodged in the back of mind. But while I've been writing up assignments, I look at my code and think, "I can't reuse this."

I had read some about polymorphism and heard it mentioned in java lectures on youtube. Then during class on Tuesday, my teacher went over the concepts. The usefulness of it dawned on me. Awesome.

I'm sure I'm just scratching the surface too.

This post has been edited by jeffindenver: 22 October 2015 - 07:25 AM


Is This A Good Question/Topic? 2
  • +

Replies To: Polymorphism - I love it

#2 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Polymorphism - I love it

Posted 22 October 2015 - 07:59 AM

It's good to learn to love polymorphism. Later on you'll find that it's possible to over-do it, and that it can be a productive source of WTFs and gotchas as well as a means to clarity and convenience, but there's certainly a reason people use it.
Was This Post Helpful? 2
  • +
  • -

#3 jeffindenver  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 114
  • Joined: 07-August 15

Re: Polymorphism - I love it

Posted 22 October 2015 - 01:35 PM

My instructor gave a similar warning. He said we should design classes for inheritance or else avoid it.

For now I'm just happy I can spend less time writing for loops with printlns inside (the rough life of a student).
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Polymorphism - I love it

Posted 22 October 2015 - 02:45 PM

I'm glad it clicked for you!

View Postjeffindenver, on 22 October 2015 - 04:35 PM, said:

He said we should design classes for inheritance or else avoid it.


I wouldn't go that far, though he might be right. In which case, you should always start with an abstract class, as the goal is an extending implementation.

Note that inheritance is NOT polymorphism, though you get it for free along a hierarchy branch.

Your next step, the one the frees you from all the dire hierarchy warnings, is interfaces. An interface IS polymorphic, as it describes the methods offered without locking down ANY implementation details.

Have fun.
Was This Post Helpful? 4
  • +
  • -

#5 jeffindenver  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 114
  • Joined: 07-August 15

Re: Polymorphism - I love it

Posted 22 October 2015 - 03:24 PM

Coding lingo can be confusing.

arguments/parameters
instance variable, ivar, member variable, field
subroutines, functions, methods, modules
elements/subscripts
derived class, child, subclass
parent class, superclass, ...

There's probably more.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Polymorphism - I love it

Posted 22 October 2015 - 06:44 PM

The language isn't confusing, for the most part. It's more that the concepts are not intuitively obvious, and there's a lot of subtle distinctions which really do make a difference. Once you learn the concepts, I suspect you'll find the terminology seems a lot easier to manage.
Was This Post Helpful? 0
  • +
  • -

#7 millerg6711  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-July 16

Re: Polymorphism - I love it

Posted 12 July 2016 - 03:48 AM

so do i
Was This Post Helpful? 0
  • +
  • -

#8 Vette  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 15-April 17

Re: Polymorphism - I love it

Posted 16 April 2017 - 07:21 AM

View Postbaavgai, on 22 October 2015 - 02:45 PM, said:

I'm glad it clicked for you!

View Postjeffindenver, on 22 October 2015 - 04:35 PM, said:

He said we should design classes for inheritance or else avoid it.


I wouldn't go that far, though he might be right. In which case, you should always start with an abstract class, as the goal is an extending implementation.

Note that inheritance is NOT polymorphism, though you get it for free along a hierarchy branch.

Your next step, the one the frees you from all the dire hierarchy warnings, is interfaces. An interface IS polymorphic, as it describes the methods offered without locking down ANY implementation details.

Have fun.


I definitely second the idea of studying interfaces, and would certainly advice looking into them if you have not already. Personally, I think they better represent polymorphism in many instances than an abstract class (since, as noted, inheritance/extending a class is not the same as polymorphism). I tend to think of an interface as more appropriate for connecting classes with similar behavior and abstract classes more suitable for connecting objects that are similar conceptually, rather than just behaviorally. For example, an interface ISwimmer might be more appropriate to link a Dolphin and Crocodile class, but an abstract class Reptile might be more appropriate for connecting a Crocodile and Iguana class. Admittedly, the appropriate choice is not always clear though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1