I am currently reading Design Patterns - Elements of Reusable Object-Oriented Software. I am in chapter 1 at page 16 in section Class versus Interface Inheritance. There in the last line of the page it says " An object can have many types, and objects of different classes can have the same type. "

I think I understand the second part of the sentence, how objects of different classes can have same types. It is possible by inheritance, if Base class and derived class have same interface.

My question is how can object have many types. Is it trying to say a derived object can have two type base and derived?

  • 3
    If you have a class Foo and another Bar which extends Foo, then an object of type Bar can also be considered of type Foo, because it inherits all of Foo's methods and data members. In some languages like C++ a class can extends multiple base classes, which may also be what the author means. – David Etler Jan 27 '16 at 22:47
  • 3
    How can I be a person, and a mammal, and an animal, and a programmer? What my traits are, or what I can do is not the definition of who I am. Likewise, objects and their types. – Telastyn Jan 27 '16 at 23:52
  • There is a notion of the true, actual, known type of a given object, which is just one single class, (even though the object can act as a substitute for base classes it inherits from and for interfaces that it implements). And that one true/actual type comes from the new xxx() expression that created it. You can see this true/actual type in most debuggers when you're looking at an object. – Erik Eidt Jan 28 '16 at 0:45
  • 2
    @Telastyn ur comment is quite intriguing. So when you say I can be person, mammal, animal, programmer, dad, brother etc. are referring them as types? – solti Jan 28 '16 at 4:57
  • 1
    @solti - that is the analogy, yes. – Telastyn Jan 28 '16 at 12:29

The big hint is in your question: "interface inheritance".

Basically, an interface is nothing but a set of method signatures. In a traditional OOP language, the only thing a class needs to do to satisfy an interface is to have implementations of those method signatures, and declare that it implements the interface. Since it's not inheriting implementations, there's no ambiguity or contradiction in having a single class implement several interfaces at once, or having several classes implement the same interface with different underlying data or algorithms.

For instance, here's some trivial Java code that declares a class which implements several other interfaces, and thus has all of their types at the same time:

public interface A { void a(); }
public interface B { void b(); }
public interface C { void c(); }
public interface D { void d(); }

public class IAmManyTypes implements A, B, C, D {
  public void a() {}
  public void b() {}
  public void c() {}
  public void d() {}
  • Thank you Ixrec .. this makes sense ... but doesn't inheritance also serves similar purpose? ( I was just curious why writer focused in interface inheritance ... in c++ we could do multiple inheritance) – solti Jan 27 '16 at 22:57
  • @solti: Java interfaces are simply gimped abstract classes. Which should lead you to the answer. – Deduplicator Jan 27 '16 at 23:09
  • @Deduplicator ... and in reducing their power, it makes them easier to reason about and thus fewer problems of a certain set of confusions about inheritance. – user40980 Jan 27 '16 at 23:15
  • Also note that an abstract class does not need to provide implementations for methods on its interfaces. – user22815 Jan 27 '16 at 23:19
  • 2
    @solti The other comments sort of cover it already, but the reason is simply that there are no difficulties in inheriting from multiple interfaces. Whereas inheriting multiple implementations raises questions about which implementation you actually want for each method, how to combine/nest/chain the constructors/destructors and other state management functions, and so on. – Ixrec Jan 27 '16 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.