Java | Polymorphism

05 min read

Polymorphism:

1. It is the ability to create a variable, a function or an object that has more than one form.

2. First of all, polymorphism works with derived class objects. A single variable of base class type can be created and made to reference objects of any derived class and to automatically call the method that is specific to the type of the object, the variable references.

3. To get the polymorphic operation when calling a method, the method must be a member of the base class as well as any derived classes involved. A base class variable cannot call a method of the derived class if it is not a member of the base class.

4. Any definition of the method in a derived class must have the same signature (name & arguments) and same return type as in the base class and must have an access specifier that is no more restrictive. This is called method overriding.

Method Overriding: It is a technique which supports run-time polymorphism. When a method in a subclass has the same signature and return type as that of the method in a superclass, then the method in the subclass is said to override the method in the superclass. If the two methods are not identical and if only their names are similar, then the two methods are simply overloaded.

Example:

class Shape //base class
{
int area(int I);
{
System.out.printliCarea will be calculated in Circle class");
}
}
Class Circle extends Shape
{
int area(int r);
{
.....
}

Base class & subclass both have defined the area( ) method. Moreover, their arguments and return types are same. Thus, method area is said to have been overridden by subclass Circle. Had the method been defined as int area(double r), then these 2 methods are said to be overloaded.

5. The method access specifier must be no more restrictive in the derived class than in the base class. ie., if the area( ) method is defined as default in the base class, in the derived class also, it must be defined with default access mode or with public access mode. But it cannot be defined with private access mode. If in the base class, the method is defined as public, then in the subclass, it can have the only public access modifier. It cannot even have default access modifier. 

6. Deciding which overridden method to call depends on the type of the object being stored and not on the type of the reference variable.

7. Polymorphism applies only to methods and does not apply to data members. When you access a data member of a class object, the type of the reference variable always determines the class to which the data member belongs.

8. Deciding which method to call in the case of method overloading is done at compile time. For aspects that are performed at compile time, the type of reference variable matters. For aspects which are performed at runtime, the type of the reference variable matters. For aspects which are performed at runtime, the type of the object being referred to, matters.

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error