Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Interfaces Compatible With Polymorphism

Tags:

I am having trouble with the concept of interfaces interacting with polymorphic types (or even polymorphic interfaces). I'm developing in C# and would appreciate answers staying close to this definition, although i think that still gives plenty of room for everyone to put forth an answer.

Just as an example, let's say you want to make a program to paint things. You define an interface for the actor that Paints, and you define an interface for the subject which is painted, Furthermore you have some subjects which can be painted in a more specific way.

interface IPainter {   void paint(IPaintable paintable); } interface IPaintable {   void ApplyBaseLayer(Color c); } interface IDecalPaintable : IPaintable {   void ApplyDecal(HatchBrush b); } 

I can imagine making a painter similar to the following:

class AwesomeDecalPainter : IPainter   {     public void paint(IPaintable paintable) {       IDecalPaintable decalPaintable = (IDecalPaintable)paintable;       decalPaintable.ApplyBaseLayer(Color.Red);       decalPaintable.ApplyDecal(new HatchBrush(HatchStyle.Plaid, Color.Green));     }   } 

Of course this will throw if paintable does not implement IDecalPaintable. It immediately introduces a coupling between the IPainter implementation and the IPaintable that it operates on. However I also don't think it makes sense to say that AwesomeDecalPainter is not an IPainter just because it's use is limited to a subset of the IPaintable domain.

So my question is really four-fold:

  • Are interface compatible with polymorphism at all?
  • Is it good design to implement an IPainter that can operate on IDecalPaintable?
  • What about if it can exclusively operate on IDecalPaintable?
  • Is there any literature or source code that exemplifies how interfaces and polymorphic types should interact?
like image 315
hannasm Avatar asked May 27 '11 03:05

hannasm


People also ask

Is polymorphism and interface same?

Polymorphism is the abstract concept of dealing with multiple types in a uniform manner, and interfaces are a way to implement that concept. Code that interacts with an interface can interact with any type that provides that interface.

What are the two kinds of interface polymorphism?

Polymorphism in Java has two types: Runtime polymorphism (dynamic binding) and Compile time polymorphism (static binding).

Is interface runtime polymorphism in Java?

Runtime Polymorphism in JavaRuntime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time. In this process, an overridden method is called through the reference variable of a superclass.

How polymorphism is achieved with the help of interface in Java?

The first advantage is to allow interfaces to implement the multiple inheritances in a particular class. The JAVA language doesn't support multiple inheritances if we extend multiple classes in the class, but with the help of the interfaces, multiple inheritances are allowed in Java.


1 Answers

The interface of a class is meant as a tool for the "user" of that class. An interface is a public presentation for the class, and it should advertise, to anyone considering to use it, what methods and constants are available and accessible from the outside. So, as it name suggests, it always sits between the user and the class implementing it.

On the other hand, an abstract class is a tool aimed at helping the "implementor" of the classes that extend it. It is an infrastructure that can impose restrictions and guidelines about what the concrete classes should look like. From a class design perspective, abstract classes are more architecturally important than interfaces. In this case, the implementor sits between the abstract class and the concrete one, building the latter on top of the former.

So to answer your question simply, Interface is a "contract" for the code to respect. When uused this way, it applies more to inheritance that polymorphism.

Abstract classes, they define a 'type'. And when the concrete sub classes use abstract classes and redefine methods, add new ones etc ... there you see polymorphism in action.

I know this post may confuse you more, it didn't make sense to me until I learned design patterns. With a few simple patterns under your belt you will better understand the role of each object and how inheritance, polymorphism and encapsulation play hand in hand to build cleanly designed applications.

Good-Luck

like image 98
stefgosselin Avatar answered Feb 09 '23 01:02

stefgosselin