Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [polymorphism]

The tag has no usage guidance.

1
vote
2answers
89 views

Unit testing similar wrappers - or single unit test is enough?

I'm working on a service which has a lots of dependencies. The way how I call them is I'm wrapping each service client around an Adapter. Like this (I'm using Java): public abstract class AdapterBase&...
0
votes
1answer
86 views

Downcastings refactored using generics

I am willing to refactor some code that acts like a controller class executing work embedded in other classes. On one side it looks good as the controller is generic and what changes has been well ...
10
votes
4answers
431 views

Replace Conditional with Polymorphism in a proper way?

Consider two classes Dog and Cat both conforming to Animal protocol (in terms of Swift programming language. That'd be interface in Java/C#). We have a screen displaying a mixed list of dogs and ...
1
vote
3answers
176 views

What are some strategies for eliminating switches in polymorphic dynamic code spread across multiple layers?

Context: design with correlated polymorphic classes For example, let's consider the following class definitions for representing messages that we would get from some remote service: class ...
1
vote
1answer
119 views

Entities and polymorphic relationships

Lets say that I have a shelf on which I can put items that are shelfable (not sure if that is actually a word but you can understand my point). So I have the following: class Shelf { /** @var ...
0
votes
2answers
131 views

Question on open polymorphism (motivation - output data to diff file types)

Let's say I have some class, and within instances of that class I want to be able to output certain data to various file types e.g. CSV, SQL, PDF, etc. The simplest way is just a series of if ...
17
votes
9answers
2k views

How is polymorphism used in the real world? [closed]

I am trying to understand how Polymorphism is used in a real life project, but I can only find the classical example (or something similar to it) of having an Animal parent class with a methodspeak(), ...
1
vote
4answers
946 views

Why the industry prefer/use composition over inheritance? [duplicate]

I was having a discussion with few friends about inheritance and composition and what I learnt from that discussion is that the use of inheritance more or less condemned in industry nowadays and ...
0
votes
1answer
602 views

Workaround for casting method parameter interface to desired concrete type in polymorphic design?

Let's say that I have a group of report types to be created and all of them inherit from base abstract class: abstract class Report { public abstract void GenerateReport(IReportData data); ...
0
votes
1answer
184 views

Polymorphism/Inheritance design concerns

I have a FtpServerDataSeriesProvider that is used to provide data series from Ftp server files: public interface class FtpServerDataSeriesProvider { IEnumerable<DataSeries> GetDataSeries();...
6
votes
1answer
204 views

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

In Clean Code by Uncle Bob, page 124-125 he states Procedural code (code using data structures) makes it easy to add new functions without changing the existing data structures. OO code, on the ...
0
votes
3answers
419 views

How to implement an opaque handle in C#

I'm writing a library to be used by several applications. Some interfaces which my library declares and uses, and which are implemented by the application, look something like this: interface ...
2
votes
1answer
88 views

Feedback on inheritance assignment

So I've got this java assignment for college and was just wondering if anyone here could give me some feedback as to what I've been thinking some of this means. I've got a .csv file with building ...
14
votes
4answers
4k views

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

When you use the concept of polymorphism you create a class hierarchy and using parents reference you call the interface functions without knowing which specific type has the object. This is great. ...
2
votes
1answer
97 views

When should concrete public methods be used if implementing an interface?

Recently I have been trying to avoid down-casting object types from an interface type to their concrete types, and 'if' statements that check for an objects concrete type at run-time. This has made me ...
5
votes
2answers
211 views

Is wrapping a hardware resource using polymorphism going too far?

I am writing a simulation engine consisting of a number of components, each of which operates on a fixed set of shared buffers. In practice, the simulation will run entirely on the GPU. When ...
2
votes
2answers
374 views

Polymorphism based on number of arguments?

I've heard that changing a function's behaviour based on argument type is called ad hoc polymorphism: program Adhoc; function Add(x, y : Integer) : Integer; begin Add := x + y end; function Add(...
0
votes
5answers
391 views

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

Consider I have classes as follows: public class User{ private String name; //other code private String getName(){ return name; } } public class ShowUserInfo{ public void ...
1
vote
2answers
3k views

How to pass a mock as std::unique_ptr to Class under test

I am writing some test units using googletest and googlemock and I am stuck in a problem related to C++11 smart pointers and polymorphism. Suppose you have those classes: class A { public: ...
0
votes
3answers
344 views

Are objects that can pass more than one IS-A test really polymorphic?

A number of tutorials on polymorphism state that "Any object that can pass more than one IS-A test is considered to be polymorphic." I wonder what they mean by that, and if that's even a true ...
15
votes
6answers
2k views

How do you make a GUI for a polymorphic class?

Let's say I've got a test builder, so that teachers can create a bunch of questions for a test. However, not all questions are the same: You have multiple choice, text box, matching, and so on. ...
5
votes
2answers
175 views

switching implementations dynamically according to execution time estimates

I have a function that I can implement in two different ways. Each way has its own advantages, and performance depends on the arguments it will be given. Since each implementation has several short ...
2
votes
1answer
1k views

OOP metrics to determine coupling

Two of the OOP metrics, Coupling Between Object classes (CBO) and Polymorphism Factor (PF) both measure coupling. Is there an instance where of them has a higher coupling while another has a lower ...
1
vote
1answer
514 views

ORM Inheritance mapping or not, how to map shared and specific properties for a set of related, real world objects

When I was starting in OO programming, mostly Symfony and Doctrine ORM, I have been advised at least one time to apply inheritance mapping for a case as follows: For instance, a HR system which deals ...
4
votes
2answers
336 views

OOP: Switching on polymorphic types and maintaining state throughout workflow

High level: I think my design pattern is flawed. Despite implementing Polymorphism, I find myself relying on large switch statements based on derived Type within my WPF application. As I'm adding more ...
4
votes
2answers
222 views

How to overwrite function in a sub class that has unique functionality in the middle of the function compared to the parent

sorry if the title is a bit messy but I wasn't sure how to form my question. So I have some classes that I've used for a project that I'm looking to making more general so that I can reuse them for ...
1
vote
5answers
332 views

How to avoid object cast after passing a parameter?

I have a problem with implementing generic user interface interaction. I have different classes that contain data each for particular interface element. So every UserInterfaceElementComponent has ...
1
vote
1answer
456 views

If polymorphism is the ability of different types to share the same interface, is there a name for a single type that fulfills different interfaces?

Consider this example of polymorphism. I have two different API, IWrite and IRead, and then a single implementation of these. interface IRead { Entity Find(int id); } interface IWrite { void ...
0
votes
1answer
65 views

Static field injection into subclasses

Say that I have a C++ class with some fields with static storage duration, call it class A. Is there some way to use inheritance to "inject" these static fields into classes which derive from class A?...
2
votes
2answers
286 views

How to change this implementation to cover drawbacks of Mediator Design Pattern here

I am new to design patterns, here is a classic example of basic mediator pattern that has 3 problems with it, first of all take a look at the application image, diagram, code and description: We use ...
8
votes
3answers
785 views

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

I am wondering if there is a difference between Haskell's type classes and Go's interfaces. Both define types based on functions, in that way, that a value matches a type, if a function required by ...
3
votes
2answers
3k views

How many types of polymorphism are there in the Python language?

I just read an article by Luca Cardelli and he explained types of polymorphism which are: The article is named On Understanding Types, Data Abstraction, and Polymorphism. Types of Polymorphism ...
0
votes
1answer
95 views

Inheritance and polymorphysm conundrum

Okay, I'm trying to write something resembling a 3D game engine. That means I have to deal with matrix transformations and similar stuff. For any of you who don't know, you need to use three basic ...
4
votes
8answers
2k views

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

I have a large number of classes with an abstract base class (A) that contains behaviour that must be supported by all the sub classes say [B..I]. In my code, I end up with a collection of objects ...
4
votes
1answer
87 views

The bound mechanism by generics for a type variable to appear in its own bound

From Programming Languages: Principles and Paradigms By Maurizio Gabbrielli, Simone Martini The bound mechanism for type variables is fairly sophisticated and flexible. In particular, a type ...
3
votes
5answers
377 views

Polymorphism in nearly identical Classes

At the time I'm workin on a project with several Instances of an abstract class. Some functions in the subclasses are the same, some not. This would lead to code like the following. In the Code i use ...
5
votes
2answers
1k views

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

BEFORE: I have an interface that has one method definition public interface IDockable { void Dock(DockerContainerConfig config); } Everything is ok for my first implementation public class ...
5
votes
1answer
260 views

Invoking a base and derived class function, virtually

I have an event handler. It receives an event and needs to do some work on it. That work is split between common work - things that have to be done for all types of event handler - and specific work - ...
1
vote
1answer
361 views

Design Pattern or suggestion for implementing

The Problem A problem I keep running into again and again in C# is how to treat a class of things as the same in some contexts, but as different in different contexts. I have found some ways of ...
3
votes
2answers
211 views

GUI applicaton states and polymorphism

I have WinForms GUI application, which can work in two "states" with different functionality, but these states share GUI controls. I am dividing these states by using enum almost in every method, like:...
0
votes
2answers
560 views

Instantiate an engine from a second project without having a reference to the second project causing a circular reference?

Let's talk about three projects. I have a Cinema project, a Cinema.Engine project, and a Cinema.Client1 project. In the Cinema project, I have an interface ICinema and a factory class CinemaFactory. ...
0
votes
3answers
825 views

Is it bad practice to ignore some parameters while implementing a method of a interface?

Should an interface method implemented by a class always use all parameters? By interface I do not necessarily mean actual interfaces, these could be other (abstract) classes which are extended, too. ...
2
votes
2answers
292 views

What's polymorphic deployment

On pages 136 and 137 in The Clean Coder, Robert C. Martin describes how chips could be independently deployed thanks to a vtable: The solution was pretty simple. Each chip had to be decoupled from ...
6
votes
3answers
576 views

How are typeclasses in functional languages different from interfaces in OO languages?

I'm learning about paradigms of functional languages. So I've read that while Subtype polymorphism is typical for OO languages, Parametric polymorphism is typical for functional languages. But I ...
2
votes
1answer
178 views

Designing generic operations in object oriented languages

I found an interesting quote in SICP that I think is highly relevant in object oriented design: We see that, in general, a type may have more than one subtype. Triangles and quadrilaterals, for ...
5
votes
2answers
965 views

Unused function parameters and polymorphism

I had a discussion with a co-worker about interface members having parameters that some implementations don't use. Say I have an interface interface IDoctor { string GetMedicalOpinion(Age age, ...
3
votes
2answers
333 views

Can a function be polymorphic and overloaded?

In Graham Hutton's book "Programming in Haskell", he defines a function to be polymorphic, if its type "contains one or more type variables". He then defines a function to be overloaded, if its type "...
5
votes
2answers
2k views

Understanding polymorphism and interface in Java

I'm reading though some notes. And I'm not understanding the following two statements. Polymorphism means that it is always the class of the actual object at run time that determines which method ...
-2
votes
1answer
559 views

Static Polymorphism explained

Here is a code I have got from here: class Deletor { public: virtual ~Deletor() {} }; template<typename T> class Base : public Deletor { public: int Run() { return ...
1
vote
3answers
2k views

Modularity vs Polymorphism (OOD main concepts)

Booch gives 4 main concepts (principles) of OOD (see his book Object-oriented analysis and design with applications ): Abstraction Encapsulation Modularity Hierarchy Usually we encounter to these 4: ...