25 April, 2013

difference between an interface and an abstract class


Question. What's the difference between an interface and an abstract class?
Ans:

Interface
Abstract class
contains only method declaration
contains method declaration as well as method definition
Any class who is going to implement interface must have to implement all the methods declared in interface
Any class who is going to extend abstract class has to implement only the abstract method that is declared in abstract class.
It is slower than abstract class
All the methods in interface in implicitly abstract
It contains abstract method as well as non-abstract method also
They simulate multiple inheritances. Means one class can implement more than one interface.
It provides the default functionality to the sub-classes.

    
Ex: Account can be of different type. saving account or current account. If we declare account as abstract class then any class who is going to extend account class will get the default functionality of account and later according to its own requirement override the method or leave it as it is.

Account.java
package com.core;

public abstract class Account {
       private static float RATE = 4;
       private float accountNumber;
       private float principleAmount;
       private float time;
       private String name;

       public String sayHello(String name) {
              System.out.println("hello" + name);
              return "success";
       }

       public void showBalance(Account acc) {
              float interest=(acc.getPrincipleAmount()* acc.getTime() * RATE) / 100;
              float totalAmount = acc.getPrincipleAmount() + interest;
              System.out.println("balance is" + totalAmount);
       }

       public float getAccountNumber() {
              return accountNumber;
       }

       public void setAccountNumber(float accountNumber) {
              this.accountNumber = accountNumber;
       }

       public float getPrincipleAmount() {
              return principleAmount;
       }

       public void setPrincipleAmount(float principleAmount) {
              this.principleAmount = principleAmount;
       }

       public float getTime() {
              return time;
       }

       public void setTime(float time) {
              this.time = time;
       }

       public String getName() {
              return name;
       }

       public void setName(String name) {
              this.name = name;
       }
}

CurrentAccount.java
package com.core;

public class CurrentAccount extends Account {
       private static float RATE = 6;

       public void showBalance(Account acc) {
              float interest = (acc.getPrincipleAmount()* acc.getTime() * RATE) / 100;
              float totalAmount = acc.getPrincipleAmount() + interest;
              System.out.println("balance is" + totalAmount);
       }
}

AccountOperation
package com.core;

public class AccountOperation {
       public static void main(String[] args) {
              SavingAccount sa = new SavingAccount();
              sa.setAccountNumber(111);
              sa.setName("mukesh");
              sa.setPrincipleAmount(1000);
              sa.setTime(2);

              sa.sayHello(sa.getName());
              sa.showBalance(sa);

              CurrentAccount ca = new CurrentAccount();
              ca.setAccountNumber(222);
              ca.setName("amit");
              ca.setPrincipleAmount(1000);
              ca.setTime(2);

              ca.sayHello(ca.getName());
              ca.showBalance(ca);
       }
}

output
----------------

hellomukesh
balance is1100.0
helloamit
balance is1120.0

No comments:

Post a Comment