| Virtual Inheritance |
Article Index for Virtual |
Website Links For Virtual |
Information AboutVirtual Inheritance |
| CATEGORIES ABOUT VIRTUAL INHERITANCE | |
| object-oriented programming | |
| c | |
|
Virtual inheritance is a special form of Inheritance in Object-oriented Programming Languages . GENERAL Virtual Inheritance is a fundamental feature of most Object-oriented languages. It allows for a derived object to be used as its base object, but still use the overridden methods (called Method Overriding ) of the derived object while in that casting context. Virtual Inheritance is the building block of interfaces in the C++ language, a very powerful feature of the language. Newer languages such as C# and Java have since directly implemented interfaces. Virtual Inheritance is implemented by construction a Virtual Table for calls to an object. LANGUAGES C++ C++ has two uses virtual inheritance to solve both disambiguity problems caused by multiple inheritance and virtual Method Overriding . Method Overriding Normal Method Overriding is limited to the context of the objects current casting. For Example: class Human{ public int Eyes(){ return 2; } } class Mutant : public Human { public int Eyes(){ return 3; } } void main(){
} Calling m->Eyes() would return 3, but calling h->Eyes() would return 2, even though they represent the same object. Simply changing the Human class definition to:class Human{ virtual public int Eyes(){ return 2; } } results in both calls to m->Eyes() & h->Eyes() returning 3.Multiple inheritance Virtual inheritance in C++ solves some of the problems caused by Multiple Inheritance by having clarifying disambiguity over which Ancestor Class members to use. It is used when the inheritance is representing restrictions of a set rather than composition of parts. You denote a member as virtual by the virtual Keyword .For example: class Animal { public: // or protected: virtual void Eat(); }; // Two classes virtually inheriting TheCommonBase: class Mammal : public virtual Animal { virtual const Color GetHairColor() const; }; class WingedAnimal : public virtual Animal { virtual void Flap(); }; // A bat can Eat class Bat : public Mammal, public WingedAnimal {}; In the above, if Mammal and WingedAnimal did not inherit virtually, a call to Bat::Eat() would be ambiguous and hence a compile error.C# C# uses virtual inheritance the same way C++ does for Method Overriding . Differing from C++, C# requires the keyword override modifier.Translating the C++ example to C#: class Human{ public virtual int Eyes(){ return 2; } } class Mutant : public Human { public override int Eyes(){ return 3; } } public static void main(){ Mutant m = new Mutant(); Human h = m } Calls to both m.Eyes() and h.Eyes() would return 3.Java In Java , all class methods are virtual: no keyword is necessary. The Java translation of the C++ is: public class Human{ public int Eyes(){ return 2; } } public class Mutant extends Human { public int Eyes(){ return 3; } } public class Program { public static void main(String {Link without Title} args){ Mutant m = new Mutant(); Human h = m System.out.print(m.Eyes()); System.out.print(","); System.out.println(h.Eyes()); } } The result of this program running is the output 3,3. |
|
|