Word About Multiple Inheritance

Visual Basic .NET doesn't support multiple inheritance—there can be only one Inherits keyword per class. In some object models (notably C++), multiple inheritance is used as a way to allow, for example, a Dog object to derive from both Animal and Pet. Single inheritance isn't a terrible limitation, and it eliminates the possibility of method ambiguity. For example, if Dog is derived from both Animal and Pet, and if both hierarchies have a method MakeNoise, there could be ambiguity over exactly which method should be called.

You can get around this single inheritance restriction in many ways. In this case, Animal could be used as the base class, Pet could be derived from Animal, and Dog could be derived from Pet. This is not multiple inheritance because there is only a single Inherits at each level. (This solution would eliminate the PetRock class because a Pet Rock might be considered a pet, but it isn't an animal.)

An alternative solution is to create Animal, from that derive a Dog class, and then also have the Dog class implement the Pet interface. An interface is like a class except that it contains only methods, and the methods aren't implemented at the interface level. A class can implement an interface simply by declaring that it does implement the interface by using the Implements keyword and by providing methods that match each of the methods in the interface. Methods can implement any number of interfaces.

0 0

Post a comment