Java – Polymorphism


  • Polymorphism is the concept where object behaves differently in different situations.
  • Defining an Object(class) that shows different functionality with same name.

Polymorphism is of two types:

  1. Compile time polymorphism
  2. Runtime polymorphism

Compile time polymorphism:

  • It is method overloading technique.
  • Defining multiple methods with same name and different signature(parameters).
  • Parameters can be either different length or different type.
  • Overloading belongs to single class(object).
class Calculator
            void add(int x, int y) {
                        int sum = x+y;
                        System.out.println(“Sum of 2 numbers is : ” + sum);
            void add(int x, int y, int z) {
                        int sum = x+y+z;
                        System.out.println(“Sum of 3 numbers is : ” + sum);
class Main
            public static void main(String[] args) {
                        Calculator calc = new Calculator();
                        calc.add(10, 20);
                        calc.add(10, 20, 30);

println() method is pre-defined and overloaded. Hence it can print any type of data:

class Overload
            public static void main(String[] args) {

Can we overload Constructor?

  • Yes allowed.
  • Overloaded constructors can connect using this() method is called Constructor chaining.

Can we overload main() method ?

  • Yes.
  • JVM invokes only standard main() method.
  • Other main() methods must be called manually like other methods in application.
class Pro
            public static void main(String[] args) {
                        System.out.println(“Standard main invoked by JVM”);
                        Pro obj = new Pro();
            void main(){
                        System.out.println(“No args main”);
            static void main(int x){
                        System.out.println(“One arg main”);

Can we write main() as ‘static public’ instead of ‘public static’?

  • We can specify modifiers and access modifiers in any order. All the following declarations are valid in java.
public static final int a;static public final int a;final static public int a;
final public static int a;static final public int a;public final static int a;
Scroll to Top