|
Written by Mukesh Ranjan
|
|
Wednesday, 23 May 2007 |
What is Polymorphism?
Polymorphism means "any forms."
In OOPS, it refers to the capability of objects to react differently to the same method. Polymorphism can be implemented in the Java language in the form of multiple methods having the same name. Java code uses a late-binding technique to support polymorphism; the method to be invoked is decided at runtime.
TYPES : Overloading and Overriding
OverloadingMethods having same name but different argument lists.Argument may differ in types or number or both.
OverridingA subclass can redefine a method that it inherits from its super class.The overriding method must have the same name, arguments, and return type as the overridden method.The overriding method cannot be less public than the overridden method. The overriding method should not throw new or broader checked exceptions that are not declared by the original method. In the following example, the overridden version of the print() method is invoked because the invoking object is an instance of the derived class:
class Base {
void print() {
System.out.println("Base");
}
}
class Derived extends Base {
void print() {
System.out.println("Derived");
}
public static void main(String args[]) {
Base obj = new Derived();
obj.print();// "Derived" is printed
}
}
To invoke the superclass version of an overridden method from the subclass, use super.methodName(). In the above example, the subclass can use the functionality of the superclass print() method by calling super.print(). Methods declared as final cannot be overridden by subclasses. Even though constructors can be overloaded, they cannot be overridden because they are not inherited.
More infoFor more java related stuff visit Mukesh Ranjans site here
submit your Article, Tutorials or FAQs
Hello... if you are interested to submit your Article, Tutorials or FAQs on TechXcel.com - provides technical excellence... mail us at
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
Copyright 2007. All Rights Reserved. |
|
Last Updated ( Wednesday, 23 May 2007 )
|