Why does an object's type refer to its interface? (Design Patterns: Elements of Reusable Object-Oriented Software book)

127 Views Asked by At

Why does object's type refer to its interface? Why the term type is used here? In terms of C++ I am not able to understand it.

Gamma, Erich. Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley Professional Computing Series) (Kindle Locations 593-596). Pearson Education. Kindle Edition.

An object’s class defines how the object is implemented. The class defines the object’s internal state and the implementation of its operations. In contrast, an object’s type only refers to its interface—the set of requests to which it can respond. An object can have many types, and objects of different classes can have the same type.

2

There are 2 best solutions below

0
On BEST ANSWER

Why does object's type refer to its interface? Why the term type is used here? In terms of C++ I am not able to understand it.

Objects in OOP are not very different from the real world. For example :

  1. A Car IS-A Vehicle. By this definition, a Car has the ability to transport people/cargo from one place to another.
  2. A Car is also a Car. By this definition, it has the ability to be driven using a steering wheel.

In the above example, a Car IS-A Car and a Car is also a Vehicle because it can be driven using a steering wheel to move cargo/people from one place to another. In other words, the type of an object in the real world is defined by the things you can do with it (vis-à-vis it's interface.)

If we use the above analogy in programming, Car is a subclass of Vehicle and code that has a Car object can use all functions from Vehicle as well as Car. This would mean that a Car IS-A Vehicle and a Car. In conclusion, the type of object is defined by its interface, i.e the set of operations it supports.

0
On

An oversimplification...

  1. Interface - a list of things that a class have and the things that it can do... a list of things that answer the "Whats"

  2. Implementation - answers the question on "How" the "Whats" are accomplished.

Example: An interface IPackageMover that does 2 things and 2 classes (types) that actually implements the interface (and also do other things aside from the interface requires)

 // the interface
 public interface IPackageMover
 {
    string GetName();
    void public void MoveTo(Package package, string newAddress); 
 }

 // the "type" that has the implementation
 public class JoeThePackageMover : IPackageMover
 {
    public string GetName()
    {
        return "Joe";
    }
    public void MoveTo(Package package, string newAddress)
    {
        PickUp(package);
        Drive(newAddress);
        DropOff(package);
    }
    public void PickUp(Package package)
    {
        // do stuff
    }
    public void Drive(string newAddress)
    {
        // do stuff
    }
    public void DropOff(Package package)
    {
        // do stuff
    }
 }

 // another "type" with the same interface
 public class PassTheBuckPackageMover : IPackageMover
 {
    public string GetName()
    {
        return "What do you want it to be?";
    }
    public void MoveTo(Package package, string newAddress)
    {
        var joe = new JoeThePackageMover();
        joe.MoveTo(package, newAddress);            
    }
    public void Chill()
    {
        //do stuff
    }

 }