| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
|
Advertisement
|
What are method overloading and method overriding?Each method in a Java class is uniquely identified by its method signature. A method's signature is its name and the number and the type of its arguments. The method return type, method modifiers, and declared thrown exceptions have no effect on the method signature. A class cannot contain two methods with the same "method signature." What is method overloading?With the Java language, you can overload methods. Overloading is the practice of supplying more than one definition for a given method name in the same class. The compiler will automatically select the most appropriate one based on the arguments with which it is called. Several restrictions govern an acceptable set of overloaded methods:
Method overloading is generally used where a class can perform the same operation on more than one type of data. What is method overriding?A sub class inherits methods from a super class. Sometimes, the sub class may want to provide a new version of methods defined in the super class. This is referred to as method overriding. Method overriding allows a sub class to provide its own implementation of a method already provided by one of its super classes. A sub class method is provided with the same signature and return type as a method that is declared in its super class, the subclass is said to override that method. You can also override a method with the same signature that returns a subclass of the object returned by the original method. This facility (introduced in 5.0) is called covariant return type. The subclass method does not inherit the functionality of its super class method unless it explicitly calls the super class method using the "super" keyword. Several restrictions govern any particular method to override another correctly:
ExampleConsider an example from 8.4.10.2 Example: Overloading, Overriding, and Hiding. In the example:
the class In this example, the members of the class Which of these overloaded
|