神刀安全网

Polymorphism in Java

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.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Polymorphism in Java

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址