Saturday, 17 August 2013

Abstraction simplified

Abstraction


Abstraction is an essential element of object oriented programming. Abstraction is used to manage the complexity of an implementation. Through Abstraction, relevant details are shown and irrelevant details are hidden.
For example, A car is driven by a person without knowing its internal architecture and construction. Here the relevant work for a person is to drive so other details like that of engine is not needed to be provided to him and thus abstraction comes into the picture.
The abstraction can be achieved by hierarchical classification of a complex system.
For example, If we take the example of a Car as a single object. It consists of several sub systems such as Seat, Brakes, Steering, Engine etc. and similarly these subsystems are again made up of more specialized parts.

Abstraction implementation in Java

In Java, Abstraction is achieved by declaring the methods without any implementation in interface or class and then defining these methods in the derived classes. This simply means that the actual definition of what a method will do is left for implementation on the class which can define the method according to the need. Abstraction is used when we know that something has to be there but not sure how exactly it should look like.
there are two ways to implement and attain the abstraction.
1. Abstract Class
2. Interface

1. Abstract Class : 

                             An Abstract class is a class of which no instance is created. To use an abstract class, it needs to be extended and the derived class must not have any abstract method. Abstract methods are declared in the abstract class which does not contain any method body. To use this method, it needs to be overridden in the sub class extending this abstract class.

For example :
When we have to create a class Shape, we know that each shape must have the method called area() but it may have different implementation for different types of shapes
skeleton for Abstract class is as explained below :
one abstract class Shape is created with two abstract methods area() and perimeter().

Abstract class Shape 








The Circle and Rectangle class extends this abstract class and they need to implement the abstract methods of Shape class. A shown below, both methods are implemented as per the type of shape. Her we can see that every shape needs to have its area and perimeter, thus these are defined in the abstract class and then implemented in the derived class and every subclass of this base class will have to implement these methods.

Derived class Circle

Derived class Rectangle

2. Interface: 

                   It is another way to implement Abstraction in java. Interfaces are just like classes with its all member variables defined as public, final and static i.e. no instance variables and all methods as abstract and public. It is denoted with keyword interface. The classes can implement any number of interfaces and any number of classes can implement same interface. the implementing class must define all the methods declared in the interface. Interfaces are created when we know that what a class must do but not how to do.
Like Abstract class, interface van not have any concrete method in it.
An interface abstraction is implemented like as below:
Shape is one interface and Circle and Rectangle are two classes which implement this interface.

Interface definition

Circle class implements Shape interface


Rectangle class implements Shape



.






















































When to Use Interface and When to use Abstract Class

  1. When we know that one functionality should be same across all the sub classes, Abstract class is to be used. For example : Every vehicle must have a method called start() which should be their in every vehicle derived     and its implementation must be same.
  2. When we use Interface, there is always a possibility that we can implement more than 1 interfaces thus multiple inheritance can be used.

No comments:

Post a Comment