CSC/ECE 517 Fall 2011/ch1 1i zf: Difference between revisions
No edit summary |
No edit summary |
||
Line 32: | Line 32: | ||
{| border=1 align = center | {| border=1 align = center | ||
|- <b>ClassRoom’s Methods</b> (Room instance methods) | |- | ||
| <b>ClassRoom’s Methods</b> (Room instance methods) | |||
|- | |||
| Room_Number | | Room_Number | ||
|- | |- |
Revision as of 02:06, 7 September 2011
CSC/ECE 517 Fall 2010/ch1 1i zf
Introduction
In OO languages, method (http://en.wikipedia.org/wiki/Method_(computer_programming)) is a subroutine which associates with the instances of the class to define the specific behavior of this class. In the inheritance of these languages, methods reimplementation is essential because descendant class inherited from the ancestor class in which properties and method have been defined to some extent. With the method reimplementation, coding can be more efficient and simple without rewriting the same code again and agian. However, in different OO languages, the ways of acquiring or allowing method reimplementation are significantly different. In this article, we are going to introduce these differences and provide code for better understanding.
Principle of method reimplementation in descendant class
Method reimplementation is various in different OO languages according to their own programming rules and complier environments. And it also associated with inheritance between ancestor class and descendant class. Several terms of method are listed below.
Abstract method
Abstract method is a method only has name but little or no implementation in the ancestor class, which must be also an abstract class. In most of the case, codes in abstract method are considered dummy but it does not mean useless. In fact, it provides a place for descendant class which inherited from the ancestor class to define specific behaviors and actions.
For example, a toy company develops an ancestor class called “animal” which contains “dog”, “bird” and “fish” as its descendant classes. These subclasses have same characteristic such as color, size, price and behavior like move, speak. Here we use the abstract methods for move and speak so that the subclasses can define their own behaviors which act differently.
Virtual method
Virtual method, or virtual function, is the method that first declared or defined in the ancestor class and then completed defined in the descendant class. Based on the functionality in the subclasses, virtual method can be simply defined or overridden with details. In a virtual method in which only has the name but no implementation, we call this as pure virtual method, as known as abstract method introduced above.
Overridden method
Overridden method, as mentioned in virtual method, is used in OO language involving inheritance hierarchy. It allows descendant classes to redefine the method in their ancestor class with more details and particular purposes. “Override” means the implementation in subclasses rewrites or redefines the original implementation of the method in their super class without changing the name, parameter and return type. To better demonstrate how overridden method work, we take the toy company example again. In the behavior aspect of “animal”, we have the method call “move”. In this method, we know it define the same of movement for all the animals. However, the way of movement among “dog”, “bird” and “fish” are different. Thus, in “dog” class, we may redefine the detail in move method as “I use leg”; in “bird” class, the detail of move method is “I use wings”; and in “fish” class, the detail of move method is “I use fin”.
Overloaded method
Overloaded method means that method share the same name with other method but may be different in other aspects such as parameter, number of parameter and data type. And the correct method will be executed automatically during the runtime depends on how it is called.
Method reimplementation in different languages
Smalltalk
In Smalltalk language, we can use the inheritance to reimplementation the methods. Inheritance is very useful in Smalltalk which makes the Smalltalk language reusability and extensibility. There are two ways to modify the behavior of the methods in Smalltalk by inheritance. One is adding new methods and the other is overriding the inherited method.
Adding Methods
Adding new method is very simple; we can add either instance or class methods, to the class definition. The following diagrams show how the adding method works:
ClassRoom’s Methods (Room instance methods) |
Room_Number |
Room_Size |
Open_Window |
Open_Door |