CSC/ECE 517 Fall 2011/ch1 1i lj: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
---- | ---- | ||
= Introduction = | = Introduction = | ||
Method overriding [http://en.wikipedia.org/wiki/Method_overriding], 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. | |||
=Overriden Method= | |||
Method overriding[http://en.wikipedia.org/wiki/Method_overriding], 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. | |||
==<b>Ruby</b>== | |||
==<b>Java</b>== | |||
==<b>C++</b>== | |||
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: | |||
<pre> | |||
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"); | |||
} | |||
}; | |||
<pre> | |||
==<b>C#</b>== |
Revision as of 21:11, 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"); } };C#