Skip to main content
90 votes
Accepted

Polymorphism case study - Design pattern for morphing?

I know that this is meant to be a fairly contrived example to demonstrate the idea at play. But I would say that as a rule of thumb, you want to exhaust every possible option before relying on Subtype ...
Nathaniel Pisarski's user avatar
69 votes

What is polymorphism if you can already have methods that are the same defined in different types?

In your example, you don't really show the same message, you show two different messages that happen to have the same name. Polymorphism requires that the sender of a message can send it without ...
Karl Bielefeldt's user avatar
37 votes
Accepted

Separating business logic from data tempts me to use instanceof

Your problem is that you're trying to use both rich domain models I always try to use polymorphism and anaemic domain models I try to separate data from business logic at the same time. Both are ...
Philip Kendall's user avatar
36 votes

How is polymorphism used in the real world?

Stream is great example of polymorphism. Stream represents a "sequence of bytes that can be read or written". But this sequence can come from file, memory, or many kinds of network connections. Or it ...
Euphoric's user avatar
  • 37.7k
33 votes

How to handle the methods that have been added for subtypes in the context of polymorphism?

This is a good question and it's the kind of trouble a lot of people when trying to understand how to use OO. I think most developers struggle with this. I wish I could say that most get past it but ...
JimmyJames's user avatar
  • 28.5k
33 votes

What is polymorphism if you can already have methods that are the same defined in different types?

Polymorphism is the ability to have objects of different types understanding the same message This seems like a rather poor explanation of polymorphism to me. Technically correct but not very ...
JimmyJames's user avatar
  • 28.5k
29 votes

Why the industry prefer/use composition over inheritance?

TL;DR Attempting to use composition first, before attempting to use inheritance, prevents naive mistakes. Also, Class Inheritance: Is easy to misuse. Is less versatile than composition. Is less ...
Theraot's user avatar
  • 9,201
22 votes

Polymorphism case study - Design pattern for morphing?

The person, whether married or not is still the same person. Morphing it to another kind of person with a copy replace approach does not maintain this fundamental identity. Several other ...
Christophe's user avatar
  • 79.9k
22 votes

Separating business logic from data tempts me to use instanceof

Though Philip Kendall's answer has a point, I think it is oversimplifying. One can separate data from behaviour and still use object oriented means and polymorphism. If you need your data (Bar, Baz) ...
Doc Brown's user avatar
  • 212k
21 votes
Accepted

Should you make use of 'accidental polymorphism'?

Your example is a bit too complicated to make your point. You suggest dynamic dispatch, which actually works pretty much like that... if types get in the way and compile-time is a thing, name-mangling ...
Vector Zita's user avatar
  • 2,472
20 votes
Accepted

Is ad-hoc polymorphism a good practice in functional programming?

It seems to be a common cargo cult today to ask if something is "a good practice". Usually, this is the wrong question, since in programming there is almost nothing "good" or "...
Doc Brown's user avatar
  • 212k
19 votes
Accepted

How to handle the methods that have been added for subtypes in the context of polymorphism?

Depends. Unfortunately there is no generic solution. Think about your requirements and try to figure out what these things should do. For example, you said in the morning different animals do ...
Robert Bräutigam's user avatar
18 votes

Is it ok to have an empty abstract class to make concrete classes polymorphic

public interface IDockable<T> { public void Dock(T config); } By having a type parameter, you can specify the parameter you want to accept within the Dock method in your classes. Example: ...
Nathangrad's user avatar
15 votes
Accepted

Avoiding instanceof vs abstract God Class. Do I have an alternative?

Don't avoid things just because they are considered evil - understand first why they are considered evil, and then decide how to avoid that evilness. You are warned about these evil things because ...
Idan Arye's user avatar
  • 12.1k
15 votes

How do you make a GUI for a polymorphic class?

You can use a visitor pattern: interface QuestionVisitor { void multipleChoice(MultipleChoice); void textBox(TextBox); ... } interface Question { void visit(QuestionVisitor); } ...
Winston Ewert's user avatar
15 votes
Accepted

Good design when polymorphism isn't possible

The problem is we can't modify the components to implement a polymorphic method to display the correct window since it's a extern library. You may not be able to modify the component, but you can ...
Philip Kendall's user avatar
14 votes
Accepted

How to retain the concrete type when writing base-class-oriented code?

This is solved with generics. internal static class Sorter { public static IEnumerable<T> Sort(IEnumerable<T> items) where T : IHasTitle { return items.OrderBy(item => ...
John Wu's user avatar
  • 26.6k
12 votes
Accepted

How to go ahead with methods that only one derived class implements from a common interface?

How to go ahead with cases in which ONLY one derived class needs to implement a method from a common interface? If you end up in this situation then generally you have a problem with your design ...
Ewan's user avatar
  • 78.5k
11 votes

In what ways is Polymorphism used (other than in arrays)?

The point of polymorphism isn't arrays. It's that you can send an object a message without knowing exactly what kind of object you're talking to. out.print("Hello world"); This might seem like it ...
candied_orange's user avatar
11 votes

Should you make use of 'accidental polymorphism'?

The philosophical question of the day: is it really accidental ? You are telling us as example, that a switch processes events, and that you realise how some of these processing methods have the ...
Christophe's user avatar
  • 79.9k
10 votes

Avoiding instanceof vs abstract God Class. Do I have an alternative?

It seems you have already recognized you could use some polymorphic behavior. You have collections of objects and it would be handy to just order each object to "DoTheThing()" even though the details ...
COME FROM's user avatar
  • 2,686
9 votes

Is it ok to have an empty abstract class to make concrete classes polymorphic

Implementation specific parts shouldn't be part of the interface. Instead take every specific config as a constructor parameter. public interface IDockable { void Dock(); } public class ...
Spotted's user avatar
  • 1,690
9 votes

How to handle the methods that have been added for subtypes in the context of polymorphism?

TL;DR: Think about an abstraction and methods that apply to all subclasses and cover everything you need. Let's first stay with your eat() example. It's a property of being a human that, as a ...
Ralf Kleberhoff's user avatar
9 votes

How to tackle extensibility considering the Data/Object Anti-Symmetry?

This has been termed the Expression Problem by Phil Wadler, although it is much older than the discussion in which he came up with this term. Solving it is one of the "holy grails" of Programming ...
Jörg W Mittag's user avatar
9 votes

Replace Conditional with Polymorphism in a proper way?

You're letting the protocol type itself determine behavior. You want to treat all protocols the same throughout your program except in the implementing class itself. Doing it this way is respecting ...
Neil's user avatar
  • 22.8k
9 votes

Polymorphism case study - Design pattern for morphing?

The State Pattern could be applied here. But whether there is a better way to model the relationship depends entirely on the needs of the using code. You haven't offered any using code so all I can do ...
candied_orange's user avatar
9 votes

What is polymorphism if you can already have methods that are the same defined in different types?

So circle1 and rectangle1 understood the same message draw() without using Polymorphism! What makes you think they are not using Polymorphism? Am I missing something? Yes: that what you describe ...
Jörg W Mittag's user avatar
8 votes

What is the difference between Haskell's type classes and Go's interfaces?

There are several differences Haskell typeclasses are nominatively typed -- you have to declare that Maybe is a Monad. Go interfaces are structurally typed: if circle declares area() float64 and so ...
walpen's user avatar
  • 3,241
8 votes
Accepted

switching implementations dynamically according to execution time estimates

The standard approach for, say, sorting a list is to use quicksort for lists greater than a threshold k and insertion sort otherwise. So partitioned lists using quicksort of a certain dimension are ...
Neil's user avatar
  • 22.8k
8 votes
Accepted

Should I follow "dependency inversion principle" rule even if I need one type only and don't need polymorphism?

Yes, but only if you need it. The Dependency Inversion Principle is not about making something work now. It talks about abstractions and concretions but it's not really about that either. It's ...
candied_orange's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible