What is polymorphism? The meaning of the word polymorphism is something like one name, many forms.
How does Java implement polymorphism? Polymorphism manifests itself in Java in the form of multiple methods having the same name. In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods).
In other cases, multiple methods have the same name, same return type, and same formal argument list (overridden methods).
Three distinct forms of polymorphism From a practical programming viewpoint, polymorphism manifests itself in three distinct forms in Java: • Method overloading • Method overriding through inheritance • Method overriding through the Java interface
Method overriding through the java interface:
One object, many types..
Furthermore, because a single class can implement any number of different interfaces, a single object instantiated from a given class can be treated as any of the interface types implemented by the class from which it is instantiated. Therefore, a single object in Java can be treated as many different types. (However, when an object is treated as an interface type, only those methods declared in that interface can be invoked on the object. To invoke other methods on the object, it necessary to cast the object's reference to a different type.)
Treating different types of objects as a common type..
All of this also makes it possible to treat objects instantiated from widely differing classes as the same type, provided that all of those classes implement the same interface. Important: When an interface method is invoked on one of the objects using the reference of the interface type, the behavior of the method will be as defined by the author of the specific class that implemented the interface. The behavior of the method will often be different for different objects instantiated from different classes that implement the same interface.
1 comment:
What is polymorphism?
The meaning of the word polymorphism is something like one name, many forms.
How does Java implement polymorphism?
Polymorphism manifests itself in Java in the form of multiple methods having the same name.
In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods).
In other cases, multiple methods have the same name, same return type, and same formal argument list (overridden methods).
Three distinct forms of polymorphism
From a practical programming viewpoint, polymorphism manifests itself in three distinct forms in Java:
• Method overloading
• Method overriding through inheritance
• Method overriding through the Java interface
Method overriding through the java interface:
One object, many types..
Furthermore, because a single class can implement any number of different interfaces, a single object instantiated from a given class can be treated as any of the interface types implemented by the class from which it is instantiated. Therefore, a single object in Java can be treated as many different types.
(However, when an object is treated as an interface type, only those methods declared in that interface can be invoked on the object. To invoke other methods on the object, it necessary to cast the object's reference to a different type.)
Treating different types of objects as a common type..
All of this also makes it possible to treat objects instantiated from widely differing classes as the same type, provided that all of those classes implement the same interface.
Important: When an interface method is invoked on one of the objects using the reference of the interface type, the behavior of the method will be as defined by the author of the specific class that implemented the interface. The behavior of the method will often be different for different objects instantiated from different classes that implement the same interface.
Source: from net
Post a Comment