Reputation: 19
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.
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?
Upvotes: 0
Views: 41
Reputation: 56547
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.
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.
Upvotes: 1