231 Lec 18: Multiple Inheritance, Interfaces
Classes vs Interfaces
- A class defines method implementations. - An interface declares method signatures (i.e., types). - A class inherits method implementations from a superclass. - A class implements methods required by an interface. - Inheriting from multiple superclasses creates ambiguity about which method implementations to inherit. - Implementing multiple interfaces has no ambiguity because no method implementation is being inherited. *An interface acts as a supertype without being a superclass
why you should choose composition over inheritance: (4 reasons)
1. Inheritance is tightly coupled whereas composition is loosely coupled. (If the superclass's definition of a method changes, it can have an impact on the subclass, and this can be a disadvantage of inheritance.) 2. There is no access control in inheritance whereas access can be restricted in composition. (If a method in the superclass has a security hole, it can be a disadvantage of inheritance, as this hole may be exposed to the subclass.) 3. Composition provides flexibility in invocation of methods that is useful with multiple subclass scenario. (Choose the methods to expose -> control how different classes interact with each other) 4. Unit testing is easy in composition because we know what all methods we are using from another class. With inheritance, the subclass can inherit a large number of methods from the superclass, and it may not be clear which of these methods are actually being used in the subclass.
Why does single inheritance with multiple interfaces create *no ambiguity*?
Because instance variables and method implementations can only come from one source. (can only inherit from one superclass)
which languages do not support multiple inheritance (i.e only allow a class to have one superclass)?
Java, C#, Ruby, and Julia
What is virtual inheritance in C++?
a feature that ensures that only one instance of a common base class is inherited by a derived class (avoids the creation of duplicate copies of inherited data, e.g. D inherits x from A)
The *immediate superclass* of a class is....
a superclass it inherits from directly
The *transitive superclass* of a class is...
a superclass it inherits from indirectly via some other class
what is composition?
a way of designing classes that allows them to contain objects of other classes as member variables. In other words, it is a relationship between two classes where one class is a part or component of the other.
what is the diamond problem?
an ambiguity that arises when a class has multiple paths to a transitive superclass
how is multiple inheritance ambiguity resolved in C++?
by specifying copy to use By using the scope resolution operator "::" , we can specifically choose which member to use in case of ambiguity in multiple inheritance. the syntax is base_class::member_name e.g. LandWaterAnimal myAnimal; myAnimal.LandAnimal::speed; myAnimal.WaterAnimal::speed; For example, myAnimal.LandAnimal::speed accesses the speed member of the LandAnimal class, while myAnimal.WaterAnimal::speed accesses the speed member of the WaterAnimal class.
what is the default semantics for multiple inheritance in C++ ?
every subclass inherits its own copy of a variable • B inherits its own copy of x and C inherits its own copy of x • Hence, D inherits multiple copies (B::x and C::x)
how can we reuse code from multiple superclasses
multiple inheritance (supported by C++ and python)
What is *composition over inheritance*?
the principle that classes should achieve code reuse and subtyping via composition and interfaces, respectively, rather than inheritance. - use interface for subtyping - use composition for code reuse
A solution for Java and C# to support multiple inheritance is...
using multiple interfaces
use inheritance only when...
you are sure that the superclass will not be changed
what is an interface?
• An interface is a language construct used to specify a behavior that a class must implement • An interface only specifies method signatures without specifying any implementations • A class implements an interface by implementing all the methods it requires
disadvantage of multiple inheritance
• The semantics of multiple inheritance are difficult to reason about and hard to implement efficiently. • Some complications: - Ambiguity when multiple superclasses define the same instance variables and instance methods. - Diamond Problem