What is compile and runtime Polymorphism in java - OOPS principal

Polymorphism


Java allows >
  • 4.1) Compile time polymorphism &
  • 4.2) Runtime polymorphism

4.1) Compile time polymorphism

Compile time polymorphism can be achieved by using Method overloading

When a class have same method name with different argument, than it is called method overloading.


Advantage of method overloading -
Method overloading enables consistency in the naming of methods which logically perform almost similar tasks and the only difference is in number of arguments. Method overloading enables same method name to be reused in program.
Method overloading is done to make program logically more readable and understandable.

4.2) Runtime polymorphism

Runtime polymorphism can be achieved by using Method overriding

Method of superclass is overridden in subclass to provide more specific implementation.


  • Call to overloaded method is bonded at compile time.


  • Method overloading concept is also known as compile time polymorphism in java.


  • Java does not allow overloading by changing the return type, though overloaded methods can change the return type.


  • Method overloading is generally done in same class but can also be done in SubClass


Must read for detailed illustration with programs >

Method overloading in java - in detail with programs,10 Features,need of method overloading, overloading main method, Diagram and tabular form of Implicit casting/promotion of primitive Data type



Method overriding in java - in detail with programs, 10 Features, need of method overriding, understanding @Override annotation, Covariant return, diagram to understand access modifiers


RELATED LINKS>

Java tutorial & history of java >

Java tutorial - Advantage, Where is java used in real world, platform independent language



Classes, objects, interface, abstract classes and constructors >

Classes and Objects in java - Class consist of blocks, constructor, variables, methods, inner classes and inner interfaces



Interface in java - Multiple inheritance, Marker interfaces, When to use interface practically, 12 features



Abstract class in java - When to use abstract class or interface practically, 10 features



Access modifiers >

Access modifier /Access specifier in java - private, package-private(Default), protected and public - Diagram and tabular form


Method overloading and overriding >

Method overloading in java - in detail with programs,10 Features,need of method overloading, overloading main method, Diagram and tabular form of Implicit casting/promotion of primitive Data type


Method overriding in java - in detail with programs, 10 Features, need of method overriding, understanding @Override annotation, Covariant return, diagram to understand access modifiers

eEdit
Must read for you :