CSC/ECE 517 Fall 2011/ch1 1i cl: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 13: Line 13:


=== Virtual Methods ===
=== Virtual Methods ===
=== Overloading Methods===
=== Overloading ===
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.
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.



Revision as of 07:18, 7 September 2011

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. And we can put @override tag on top of a overriding mothed to have the complier check if the method is succesfully overridden.

Overriding

Virtual Methods

Overloading

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.

For example, while we have this method in the ancestor class

public int min(int n1, int n2) {
   int m;
   if (n1 < n2)
      m = n1;
   else
      m = n2;

   return m; 
}

We can still have this method in the descendant class

public int min(double n1, double n2){
   int m;
   if (n1 < n2)
      m = n1;
   else
      m = n2;

   return m; 
}

The program will invoke the right method depending on the type of the input.

But one important thing to remember is, only methods with different list of parameters are allowed to have the same names. We can not have overloaded methods based on different return types or modifiers. Different

Abstract Methods

Ruby

Advantages

C++

Java

Ruby

Disadvantages

C++

Java

Ruby

Conclusion

Reference

"Tutorials Point.com" http://www.tutorialspoint.com/java/index.htm

See also

The JavaTM Tutorials http://download.oracle.com/javase/tutorial/index.html