CSC/ECE 517 Fall 2011/ch1 1i lj: Difference between revisions
No edit summary |
No edit summary |
||
Line 57: | Line 57: | ||
}; | }; | ||
</pre> | </pre> | ||
Here's some sample code that invokes the above methods on a Base handle referencing a Derived object. | |||
<pre> | |||
void _tmain() | |||
{ | |||
Base^ r = gcnew Derived(); | |||
r->Goo(); | |||
r->Boo(); | |||
r->Doo(); | |||
} | |||
</pre> | |||
You'll get the following output :- | |||
<pre> | |||
Derived::Goo | |||
Derived::Boo | |||
Base::Doo | |||
</pre> | |||
==<b>C#</b>== | ==<b>C#</b>== |
Revision as of 21:22, 8 September 2011
CSC/ECE 517 Fall 2010/ch1 1i lj
Introduction
Overriden Method
Method overriding[1], in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.
Ruby
Java
C++
In native C++, a derived class function having the same name and parameters as a base class virtual function will *always* override it. In C++/CLI you have the option of using the new contextual keyword to specify whether you want to override a base class function or hide it. Example:
ref class Base { public: virtual void Goo() { Show("Base::Goo"); } virtual void Boo() { Show("Base::Boo"); } virtual void Doo() { Show("Base::Doo"); } }; ref class Derived : Base { public: //Overrides Base::Goo virtual void Goo() { Show("Derived::Goo"); } //Overrides Base::Boo as above virtual void Boo() = Base::Boo { Show("Derived::Boo"); } //Hides Base::Doo virtual void Doo() new { Show("Derived::Doo"); } };
Here's some sample code that invokes the above methods on a Base handle referencing a Derived object.
void _tmain() { Base^ r = gcnew Derived(); r->Goo(); r->Boo(); r->Doo(); }
You'll get the following output :-
Derived::Goo Derived::Boo Base::Doo