Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Polymorphism in Java

DZone's Guide to

Polymorphism in Java

An object showing different behaviors at different stages of its life cycle is known as polymorphism. There are two types of polymorphism: compile-time polymorphism and run-time polymorphism.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

Polymorphism is one of the pillars of OOP in Java.

An object showing different behaviors at different stages of its life cycle is known as polymorphism. There are two types of polymorphism:

1.  Compile-time polymorphism/static binding/ early binding

2. Runtime polymorphism/ dynamic binding/ late binding

Compile-time polymorphism: Here, a method's behavior is bound to the definition at the time of method compilation by the compiler. This type of binding is known as static binding, and it cannot be rebound. Example: method overloading.

Run-time polymorphism: Here, the method declaration is bound to the definition by the JVM during ,run time based on the object created. This type of binding is known as dynamic binding and it can be rebound.

Run-time polymorphism can be achieved by fulfilling the following

a)  Inheritance b) method overriding and c) upcasting

An example program of describing run-time polymorphism:

public class Account {

      void deposit(double amt)
      {
            System.out.println("to deposit the amount...");
      }

      void withdraw(double amt)
      {
            System.out.println("to withdraw the amount....");
      }

      void viewBal()
      {
            System.out.println("to view the account balance");
      }
}

public class savingAccount extends Account {

      double accBal;

      savingAccount(double initialamt)
      {
            System.out.println("opening the saving account with Rs" + initialamt);
            accBal ;
      }

      void deposit(double amt)
      {
            System.out.println("depositing  amount  Rs "+ amt);
            accBal = accBal+amt;
      }

      void withdraw(double amt)
      {
            System.out.println("withdrawing an amount of Rs"+ amt);
            accBal = accBal-amt;
      }

      void viewBal()
      {
            System.out.println("Current balance is "+ accBal);
      }
}

public class loanAccount extends Account {

      double accBal;

      loanAccount(double loanamt)
      {
            System.out.println("openning the saving account with Rs" + loanamt);
            accBal ;
      }

      void deposit(double amt)
      {
            System.out.println("depositing  amount  Rs "+ amt);
            accBal = accBal-amt;
      }

      void withdraw(double amt)
      {
            System.out.println("withdrawing an amount of Rs"+ amt);
            accBal = accBal+amt;
      }

      void viewBal()
      {
            System.out.println("Outstanding balance is "+ accBal);
      }
}

public class testAccount {

      public static void main(String[] args) {
            Account a = new loanAccount(10000.00);//upcasting
            a.viewBal();
            a.withdraw(2000.00);
            a.viewBal();
            a.deposit(4000.00);
            a.viewBal();
      }
}
Output:
opening the saving account with Rs10000.0
Outstanding balance is 10000.0
withdrawing an amount of Rs2000.0
Outstanding balance is 12000.0
depositing  amount  Rs 4000.0
Outstanding balance is 8000.0

In the above example, the properties of the Account class are inherited by savingAccount and loanAccount class. The methods deposit() and withdraw() are also overridden, and upcasting is shown in the testAccount class. Thus runtime polymorphism is achieved.

When the object of the Account class enters loanAccount, its behavior changes, and also when it enters to savingAccount, the object behavior changes.

So, the object shows different behaviors at different stages of its life cycle.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
java

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}