Skip to main content

10. Polymorphism

 

Polymorphism

  • The word Polymorphism means having many forms.
  • Any Java object that can pass more than one IS-A test can be considered polymorphic.
  • Other than objects of type Object, all Java objects are polymorphic in that they pass the IS-A test of their own type and for class Object.
  • This is because, the same object can behave as object of that class type and also their ancestor's class type.
  • class A {
        void funcA() {
            System.out.println(this);
            System.out.println("Function of class A.");
        }
    }

    class B extends A {
        void funcB() {
            System.out.println(this);
            System.out.println("Function of class B.");
        }
    }

    class ShellClass {
        public static void main(String[] args) {
            B refVar = new B();
            refVar.funcB(); // behaving as object of B
            refVar.funcA(); // behaving as object of A
        }
    }

The polymorphic behavior is not only limited to objects, but methods can also be polymorphic.

Overridden Methods

  • The key benefit of overriding is the ability to define behavior that's specific to a particular subclass type.
  • Abstract methods must be overridden by the first concrete (non-abstract) subclass.
  • Constructors cannot be overridden.
  • Final and static methods cannot be overridden. Static methods can be defined again in BaseClass but without @Override annotation, whereas final methods can't be defined again in BaseClass.
  • class ParentClass {
        static void func() {
        }

        final void fun() {
        }
    }

    class BaseClass extends ParentClass {
        @Override
        static void func() {
        }

        @Override
        final void fun() {
        }
    }

  • Only inherited methods may be overridden, and remember that private methods are not inherited.
  • A subclass uses super.overriddenMethodName() to call the superclass version of an overridden method.
  • Object type (not the reference variable's type), determines which overridden method is used at runtime.
  • class A {
        void func() {
            System.out.println("Function of A");
        }
    }

    class B extends A {
        @Override
        void func() {
            super.func();
            System.out.println("Function of B");
        }
    }

    class ShellClass {
        public static void main(String[] args) {
            A refVar = new B();
            refVar.func(); // Calling B's function
        }
    }

The rules for overriding a method are as follows:

  • The argument list must exactly match that of the overridden method.
  • The return type must be the same as, or a subtype of (known as covariant return), the return type declared in the original overridden method in the superclass.
  • The access level can't be more restrictive than the overridden method's.
  • The access level can be less restrictive than that of the overridden method.
  • The overriding method can throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception.
  • The overriding method must not throw checked exceptions that are new or broader than those declared by the overridden method.

Polymorphism applies to overriding, not to overloading.

Prev Next

Comments