CSC/ECE 517 Fall 2011/ch1 1i cl
Introduction
In O-o languages Object-oriented languages, method is a subroutine that associates with the the class and defines the behaviors performed by the instances of the class. A ancestor class may has its descendant classes which inherit all its methods' propertiess(name,return type,etc). Method reimplementation is required when the decendent class needs to change the behavior of a method which was already implemented by the ancestor class, and it is more efficient to do so other than writing a new method. In different O-o languages, the ways to require or allow a class to reimplement methods are very different. So it is important for us to know how different languages handle the reimplementation and what are the advantages and disadvantages of them.
Reimplementation in different O-o languages
C++
Java
General
In Java, we can use variaty of approaches to do the reimplementation. We still have approaches like Overridden Methods and Virtual Methods the same as what we have in C++, while there are some other convinient approaches such as Abstract Methods and Overloading Methods which C++ doesn't support.
Overridden Methods
Virtual Methods
Overloading Methods
In Java, methods with the same name are allowed as long as they take different parameters. It is very convenient to have such kind of function because sometimes our method needs to take different types of parameters, and we don't want to have a list of different method names to confuse the users.
public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }
Abstract Methods
Ruby
Advantages
C++
Java
Ruby
Disadvantages
C++
Java
Ruby
Conclusion
Reference
See also
The JavaTM Tutorials http://download.oracle.com/javase/tutorial/index.html