Polymorphism in Java
Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.
Overriding vs Overloading
- Overloaded methods supplement each other while an overriding method replaces the method it overrides
- Overloaded methods can exist in any number in the same class while each method in a base class can be overridden at the most once in any one class
- Overloaded methods must have different parameter lists while overriding methods must have identical type and order of the parameter list
- The return type of an overloaded method can be chosen freely while the return type of an overriding method must be identical to that of the method it overrides
Polymorphism
Single method call behaves differently depending on the type of the object which the call references. We can store a reference to a derived class object in a variable of the derived class type AND we can also store it in a variable of any direct or indirect base class. To be able to support polymorphic behavior, there are a few requirements as given below:
- The method call for the derived class object must be through a variable of a base class object
- The method called must also be a member of the base class
- The method being used must satisfy all requirements for overridden methods
Example 1:
Rectangle r = new Rectangle(); Ellipse e = new Ellipse(); Shape s = r; s.draw(); s=e; s.draw();
Example 2:
public class PolymorphismDemo {
public static void main(String[] args) {
m(new GraduateStudent());
m(new Student());
m(new Person());
m(new Object()); }
public static void m(Object x) {
System.out.println(x.toString());
}
}
class GraduateStudent extends Student {
}
class Student extends Person {
public String toString() {
return "Student"; }
}
class Person extends Object {
public String toString() {
return "Person"; }
}
Casting Objects
You have already used the casting operator to convert variables of one primitive type to another. Casting can also be used to convert an object of one class type to another within an inheritance hierarchy. In the preceding section, the statement
m(new Student());
assigns the object new Student() to a parameter of the Object type. This statement is equivalent to:
Object o = new Student(); // Implicit casting m(o);
Why Casting Is Necessary?
Suppose you want to assign the object reference o to a variable of the Student type using the following statement:
Student b = o;
A compilation error would occur. Why does the statement Object o = new Student() work and the statement Student b = o doesn’t? This is because a Student object is always an instance of Object, but an Object is not necessarily an instance of Student. Even though you can see that o is really a Student object, the compiler is not so clever to know it. To tell the compiler that o is a Student object, use an explicit casting. The syntax is similar to the one used for casting among primitive data types. Enclose the target object type in parentheses and place it before the object to be cast, as follows:
Student b = (Student)o; // Explicit casting
Related Posts
Popular Posts (last 30 days)
- Attendance Management System 1486 view(s)
- Advanced Java Tutorial (For Intermediate) 762 view(s)
- JAVA Graphical User Interface (GUI) 724 view(s)
- Graph Implementation in C++ 534 view(s)
- File Handling using Input-Output Streams in Java 475 view(s)
- Linked lists in C++ 456 view(s)
- Sockets and Network Programming in Java 377 view(s)
- Applications of Stack in data structures 370 view(s)
- UDP Datagram Sockets in Java 361 view(s)
- Circular Linked Lists 333 view(s)







