Differences Between Abstract Class Vs Interfaces in Java



Absract Class
Interface
When to use ? :
At the time of impementing the Inheritance if you found few common and few specific state or methods or behavior in both parent and child class, You should use abstract parent class and your parent abtract class should contain:
  1. Common methods (non-abstract) with non-private visibility so child class can access them.
  2. Abstract methods which child class are suppose to implement as per requirement.
  3. You can also put common state or behavior of Inheritance hierarchy classes here. 
When to use ? :
At the time of implementing the Inheritance when there is no any common state or method or behavior and you want certain kind of contract to be implemented by child classes by overriding the methods you should use Interfaces. Your Interface should contain:
  1. Only abstract methods (behavior only - no state).
  2. Global constants.
Inheritance implementation with Abstract or Concrete class is called Generalization.
Inheritance implementation with Interface is called Realization.
You can write both abstract and non-abstract methods in abstract class.
You can write only abstract methods in Interface as It represents a kind of Contract with Concrete class needs to implement.
You can have static and non-static variables / state or methods / behaviros in abstract class.
You can have only non-static methods / behaviros and Global constants (public static final variables).
Generalization.
Realization.
Concrete or Child class can not extend any other class because it has already been extended from Abstract parent class. But It can still implement any other Interfaces if required.
Concrete or Child class can extend any other one class if necessary and It can also implement any other Interfaces if required.
  • You can have Bank as Abstract parent class and HSBCBank, AmericanBank etc. as concrete classes.  
  • Put common methods or behaviors of banks in Bank class and specific behaviors in concrete classes.
  • You can define RuleEngine as Interface and SalesRuleEngine, FinanceRuleEngine as concrete classes in your application.
  • SalesRuleEngine and FinanceRuleEngine are following the contract defined in RuleEngine interface.

Example:
abstract class Bank {
    // common methods
    public int withdraw() {
        // method body
    }
    public boolean deposit() {
        // method body
    }
  
    // abstract methods
    public abstract void openAccount();
    public abstract void closeAccount();
}
class HSBCBank extends Bank {
    public void openAccount() {
        // method body
    }
    public void closeAccount() {
        // method body
    }
}

Example:
interface RuleEngine {
    // by default variables are public static final only
    int RULE_CONSTANT = 1;
  
    // contract methods needs to be implemented by concrete classes.
    // by default declared methods are public abstract only.
    void createRule();
    void updateRule();
    void deleteRule();
    public abstract void getAllRule();
}
class SalesRuleEngine implements RuleEngine {
    public void createRule() {
        // method body
    }
    public updateRule() {
        // method body
    }
    public void deleteRule() {
            // method body
    }
    public void getAllRule() {
        // method body
    }
}
Some important abstract classes in Java API are:
  1. AbstractList
  2. AbstractSet
  3. AbstractMap etc...
Some important Interfaces in Java API are:
  1. Serializable
  2. Clonnable
  3. List, Set, Map etc...



No comments:

Post a Comment