Understanding Object Oriented Concepts - Abstraction And Interface

Abstraction And Interface

Abstraction is one of the important feature of OOPS.It uses everywhere in real life, when we talk about any functionality example: withdrawing money from ATM machine, sending messages via email/mobile, driving car etc. 


We only use the functionalities we don’t know about how actually it is working or what is the implementation of the action. Hiding data of object and complex implementation from the user to increase the security and efficiency of data known as abstraction.

Abstract Class 

To achieve abstraction in programming, Object oriented languages provides abstract classes, that keeps essential information about the object.


Abstract class works only as the base class for its sub-classes, sub-classes shares object's properties and functions from abstract class and implements function definition accordingly.


Abstract is the base for its sub-classes that's why object cannot be initialized.


It is declared with abstract keyword. 


Abstract class has abstract methods that don’t have any body, abstract methods are implemented by its sub-classes.


Abstract class can have non-abstract or concrete methods too. non-abstract methods are the common methods with its body and implementation.

abstract class ClassName {


void concreteMethod() {}


   abstract void abstractMethod();


}//end of the class

How To Achieve Abstraction –

I have an object Shape and it has one functionality draw, implement it using abstraction.

abstract class Shape {


  abstract void draw();


}//end of the class


class Rectangle extends Shape {


   void draw() {

     System.out.println(“Drawing Rectangle”);


   }

}//end of the class


class Circle extends Shape {


   void draw() {

     System.out.println(“Drawing Circle”);


   }

}//end of the class


class Square extends Shape {


   void draw() {


     System.out.println(“Drawing Square”);

   }

}//end of the class


public class mainClass {


   public static void main(String args[]) {

     Shape shapeRec = new Rectangle();

     shapeRec.draw(); //”Drawing Rectangle”

     Shape shapeCir = new Circle();

     shapeCir.draw(); //”Drawing Circle”

     Shape shapeSqr = new Square();

     shapeSqr.draw(); //”Drawing Square”


 }

}//end of the class

   
Explanation-

As we can see that Abstract class works in inheritance scenario, where abstract class is the base class for sub classes, each sub-class implemented the function draw according their requirement.

As we know we cannot create abstract class object, so that there is no calling for abstract class method. We know that object Shape has draw method but we don't know how exactly it is working. To call draw method we have to pass sub-class reference to base class.

The way of calling sub-classes methods is known as dynamic binding. For more details on dynamic binding please read polymorphism concept of OOPS. Polymorphism In OOPS

Abstract class can have constructor as well, constructor will be called by subclass reference.

abstract class Shape {

     
      Shape() {


       System.out.println(“Drawing process starts”);

      }


      abstract void draw();


      void show() {


       System.out.println(“Process is done!”);

      }

}//end of the class


class Rectangle extends Shape {


      void draw() {


       System.out.println(“Drawing Rectangle”);

      }

}//end of the class


public class mainClass {


      public static void main(String args[]) {

       Shape shapeRec = new Rectangle();

       shapeRec.draw();

       shapeRec.show();

      }

}//end of the class


Output –


Drawing process starts

Drawing Rectangle

Process is done!
    
Interface 

Interface is to provide the implementation layer and provide the methods to be implemented by its sub-classes. There are some 
predefined interfaces that we use in our application for example List interface in collections.

Some predefined interface that doesn’t have any method to be implemented by their sub-classes but there meaning has been already defined to JVM for useful operations such type of interfaces are known as Marker Interfaces example: Serializable, Clonable etc.

Interface are very much similar with abstract classes.

Interface object cannot be initiated.

Interface are declared by interface keyword.

Interface do have only abstract methods and it will not be declared by abstract keyword. In java8 now interface can have default and static methods as well.

Any class can implement many interfaces together that is why it serves multiple inheritance in java.

interface InterfaceName {


  void defaultMethod() {} //can have body


  static void Method() {} // can have body


  void abstractMethod();


}//end of the interface


Default method is used perform any default operations to child classes for example loading resource, closing resources etc.

Interface provides abstract methods that would be implemented by child classes. If interface has 4 abstract methods than all 4 should be implemented by child class.

In java implements keyword use when a class inherit any interface and extends keyword use when interface in inherit by other interface.

interface Message1 {


   void startMsg();


}//end of the interface


//Interface extending another interface

interface Message2 extends Message1 {


    void stopMsg();


}//end of the interface


//class implements interface

class Mobile implements Message2 {


    void startMsg() {


       System.out.println(“Start Message”);

    }


    void stopMsg() {


       System.out.println(“Stop Message”);

    }


}//end of the class


public class mainClass {


    public static void main(String args[]) {


      Mobile mob = new Mobile();

      mob.startMsg();

      mob.stopMsg();


    }

}//end of the class


Output-

Start Message

Stop Message


Difference between Abstract class and Interface -

1.
Abstract class is a base class for its sub classes to provide methods to be implemented by its sub classes.

Interface is the interface that also provide methods to be implemented by its sub classes.

Both cannot be initialized as both have been designed to provide base layer to the program. 

2.
Abstract classes are declared by abstract keyword.

Interface are declared by interface keyword.

3.
Abstract class do have abstract methods (with no body) and concrete (non-abstract, like other methods) methods. Abstract methods will be declared by abstract keyword. Abstract class may have static fields and methods.

Interface do have abstract methods, methods will not be declared by abstract keyword. if it has variable the it should be public static final.

4.
Abstract class does not support multiple inheritance or sub class can extend only one abstract class. 

Interface does support multiple interface , sub class can implements more then on interfaces.

How to decide which is best Abstract class or Interface?

When use abstract class,

When we want to declare non-static, non-final fields in a base class so that their state can be change by methods.

When yow want to share data among co-related sub classes.

Abstract class are designed to provide the implementation to sub classes. 

When use Interface 

Interface are designed to provide operation to any class.

Any unrelated class can implement interfaces and can use its operations, 
example comparable , clonable , seriaziable etc.

Multiple inheritance can be manageable by using interface.


Thanks for reading,please do share and comment your suggestion.

No comments

Powered by Blogger.