CSC/ECE 517 Fall 2011/ch1 1i zf: Difference between revisions
No edit summary |
No edit summary |
||
Line 79: | Line 79: | ||
} | } | ||
</pre> | </pre> | ||
Class Say represents the superclass and implements a method call express (). The subclass called Shout will inherit all the methods which in the Say class. But the class Shout will override the method express in Shout class, and replace it with new action. | |||
<pre> | |||
Say hello_say = new Say(); | |||
hello_say.express(); // Prints “Hello wolrd!” | |||
Say hello_shout = new Shout(); //Polymorphism | |||
hello_shout.express(); //Prints “Hello wolrd, loudly!” | |||
</pre> | |||
The super can be used to call the superclass's method; we can modify the class in class Shout to make the method express call another express method which in superclass Say. | |||
<pre> | |||
public class Shout extends Say { | |||
@Override // @Override annotation in Java 5 is optional but helpful. | |||
public void express() { | |||
System.out.println(“Hello wolrd, loudly! ”); | |||
Super.express(); //will call the express method in class Say | |||
} | |||
} | |||
</pre> | |||
But some of the methods cannot be overridden. For example, in Java, a method that is declared final in the super class cannot be overridden, and the method declared private or static cannot be overridden too. We cannot make a class that that is declared final to become a super class too. |
Revision as of 02:28, 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:
Room instance methods |
Room_Number |
Room_Size |
Open_Window |
Open_Door |
ClassRoom instance methods |
Turn_On_Computer |
Clean_Blackboard |
The ClassRoom class is specific types of Room. The ClassRoom class is inheriting from Room Class, so the new methods Turn_On_Computer and Clean_Blackboard will add to the ClassRoom class. Now ClassRoom class definition can now support all of the messages or methods in Room and the two new messages.
Overridden method
Overriding an inherited method is very important way to provide unique behaviors to a class. If an object receives a message which we have a method in the class definition for that massage, the object will work its way up the hierarchy until it finds a method with that name. We cannot delete the inherited methods, if we do not need the method in the superclass, we can provide a method by the same name with the superclass in the subclass with no code in the method. In this way, we can simply replace the behavior of the superclass behavior by no behavior.
Java
We can use the inheritance to make the method reimplementation in Java. They are similar with the ways we do in Smalltalk, like the overriding and overloading. But in Java, we can define abstract method and virtual method in Java, and it provides a @override tag that prevents the programmer from thinking another method is being overridden when really it is not.
Overridden method
If we want to override a method in subclass that inherited from the superclass, we should invoke the superclass method by using the keyword super. Below is the example:
public class Say { public void express() { System.out.println(“Hello wolrd!”); } } public class Shout extends Say { @Override // @Override annotation in Java 5 is optional but helpful. public void express() { System.out.println(“Hello wolrd, loudly! ”); } }
Class Say represents the superclass and implements a method call express (). The subclass called Shout will inherit all the methods which in the Say class. But the class Shout will override the method express in Shout class, and replace it with new action.
Say hello_say = new Say(); hello_say.express(); // Prints “Hello wolrd!” Say hello_shout = new Shout(); //Polymorphism hello_shout.express(); //Prints “Hello wolrd, loudly!”
The super can be used to call the superclass's method; we can modify the class in class Shout to make the method express call another express method which in superclass Say.
public class Shout extends Say { @Override // @Override annotation in Java 5 is optional but helpful. public void express() { System.out.println(“Hello wolrd, loudly! ”); Super.express(); //will call the express method in class Say } }
But some of the methods cannot be overridden. For example, in Java, a method that is declared final in the super class cannot be overridden, and the method declared private or static cannot be overridden too. We cannot make a class that that is declared final to become a super class too.