0
  1. With regards to a class, what does an interface mean? I think it refers to all the public functions of the class. Am I correct or does it mean something else? I keep hearing it a lot but never quite noticed the explicit definition.

  2. This one's the real question. What does it mean for a derived class to retain the interface of the base class it's derived from? I think it means that the public functions in the base class must be public in the derived class as well (which will be the case in public and protected inheritance). Am I mistaken?

South
  • 19
  • 2
  • 1. Public functions **and variables**. – barak manos Apr 02 '15 at 03:59
  • 1. An interface is simply a way for other objects to communicate with an object. Oftentimes, interfaces are explicitly defined by public attributes of that object (but not all languages enforce this). 2. If an derived object retains its base class's interface, then other code that knows how to use *base class* will be able to use that knowledge to operate the *derived class* as well. – Joel Cornett Apr 02 '15 at 04:03

1 Answers1

1
  1. Yes, the interface of a class is the collection of its visible member functions to the outside world, i.e. its public member functions. Some also include member variables in the interface, but it is not usually common to have public member variables (unless declared static). Most often, interfaces are implemented via abstract base classes. This is in contrast to Java, which has a different keyword for specifying interfaces.

  2. Retaining the interface means having the public member functions in the base class visible across the class hierarchy. Also, you can override virtual functions so you get polymorphic behaviour, keeping the common interface. Note that only public inheritance preserves the interface, protected and private do not. Another way of failing to retain an interface is via name hiding in C++. Example: re-declaring Base::f(int) as Derived::f(float,float). In this case, the Base::f(int) is not longer visible in Derived, unless via a using Base::f; statement.

Community
  • 1
  • 1
vsoftco
  • 52,188
  • 7
  • 109
  • 221