79515613

Date: 2025-03-17 19:56:10
Score: 0.5
Natty:
Report link

What is polymorphism?

Poly means many and morph means shapes.

In layman terms of programming polymorphism can be called as creating many shapes of same thing.

There are two types of polymorphism:

  1. Compile time polymorphism (also called as overloading, and static binding)

  2. Runtime polymorphism (also called as overriding, dynamic method dispatching, and late binding)

Compile time polymorphism is when you create method of same name in the same class but it has different arguments.

Example:

public class TestClassOne {
    public static void main(String[] args) {
        Foo foo = new Foo();
        foo.fooMethod();
        foo.fooMethod("Hello World");
        foo.fooMethod(1);
    }
}

class Foo {
    public void fooMethod() {
        System.out.println("fooMethod");
    }
    public void fooMethod(String arg) {
        System.out.println("fooMethod with string args");
    }
    public void fooMethod(Integer arg) {
        System.out.println("fooMethod with Integer args");
    }
}

Runtime polymorphism is when you create method with same signature but in inheriting or implementing class. Which means yes polymorphism allows all methods to be overridden but it is not mandatory when class is inheriting another concrete class. However abstract methods should mandatorily be overridden by subclass/implementing class.

I do not know about other languages but Java:

  1. Does not allows to override final method.

  2. Does not allows final class to be inherited.

  3. Does not allows Interface or Abstract class to be final.

  4. Does not allows abstract method to be final.

Example


public class TestClassOne {
    public static void main(String[] args) {
        Foo foo = new Foo();
        foo.fooMethod();
        foo.fooMethod("Hello World");
        foo.fooMethod(1);

        //reference type of parent class
        //but object of child class
        Foo foo2 = new Foo2();
        foo2.fooMethod("Hello World");
        foo2.staticMethod(); //staticMethod of parent class will be called as static methods are bind to class not object

        Foo2 foo3 = new Foo2();
        foo3.staticMethod(); //staticMethod of child class will be called as reference type is Foo2
    }
}

class Foo {
    final public void fooMethod() {
        System.out.println("fooMethod");
    }
    public void fooMethod(String arg) {
        System.out.println("fooMethod with string args");
    }
    public void fooMethod(Integer arg) {
        System.out.println("fooMethod with Integer args");
    }
    public static void staticMethod() {
        System.out.println("Parent class staticMethod");
    }
}

class Foo2 extends Foo {
    @Override
    public void fooMethod(String arg) {
        System.out.println("fooMethod with string args in subclass");
    }

    public static void staticMethod() { //this is method hiding
        System.out.println("Child class staticMethod");
    }
}

/**
class Bar extends Foo {
    public void fooMethod(){ //will give compile time error as trying to override final method
        System.out.println("barMethod");
    }
}

abstract class absclass{
    final abstract void fooMethod(); //not allowed
    abstract void myMethod();
}
class concreteclass extends absclass{ //will give compile time error as it does not implement all methods
    public void myMethod(){
        System.out.println("myMethod");
    }
}
 **/

Static methods are not overridden, when you do so it is called method hiding instead of method overriding.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): What is
  • Low reputation (1):
Posted by: Yasin Ahmed