What is Abstraction in JAVA

               I will suggest you to read  Abstraction in JAVA  and  Inheritance in JAVA before reading this post.Consider the below example .
Parent class :  Loan
Child classes:  EducationalLoan and HousingLoan
             For loan,interest have to be calculated.To achieve that functionality, InterestCalculation method will be declared. Since the interest calculation differs for educational and housing loan, the logic will be different for both the subclasses. Hence only declaration will be present in the loan
class as mentioned below. If the class has unimplemented methods, then the class should be marked with abstract keyword  and the class is called Abstract class .If the method has no implementation then the method  should be marked with abstract keyword and it is called Abstract method.

    abstract class Loan{
abstract public void InterestCalculation(){
}
   }
    class EducationalLoan extends Loan{
   public void InterestCalculation(){
      // logic to calculate the interest
}

 }
  class HousingLoan extends Loan{
public void InterestCalculation(){
           // logic to calculate the interest
}
 }

  • If the class has one or more abstract methods then the class should be declared as abstract.
  • Abstract class  can have implemented methods or concrete methods [ methods with logic  defined] along with Abstract methods.
  • If the  class which inherits the abstract class and if doesnt implement the abstract method  declared in the super class,then the subclass also should be declared Abstract.
  • If the Class implementing an interface[ will discuss in upcoming post] doesnt implement the  abstract method in interface then it should be marked with abstract keyword.
  • Instance cannot be created for Abstract class. i.e objects cannot be created for abstract class
  Following cannot be marked with Abstract keyword

  Static methods
  private methods   -
  final methods    - methods with final keyword
  Constructors

Previous
Next Post »