Thursday, September 5, 2013

What is Abstraction ?


Abstraction : 

- Hiding unnecessary data from the user.

- Hiding implementation details.
- Abstraction is a process of hiding the implementation details and showing only functionality to the user.
- it shows only important things to the user and hides the internal details.


Advantages:


            - It increases security.

            - Enhancement is easy.
            - Improve maintainability.


There are two ways to achieve abstraction in java.

1) Abstract (Partially or may be Fully abstraction)
2) Interface (Fully abstraction)


An Abstract Class Example

In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects—for example: position, fill color, and moveTo. Others require different implementations—for example, resize or draw. All GraphicObjects must know how to draw or resize themselves; they just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object—for example, GraphicObject, as shown in the following figure.
Classes Rectangle, Line, Bezier, and Circle inherit from GraphicObject



An abstract method in Java doesn't have body , its just a declaration. In order to use abstract method you need to override that method in SubClass.

So when do you use abstraction ?
*** when Yo  know something needs to be there but  not sure how exactly it should look like.

*** e.g. when I am creating a class called Vehicle, I know there should be methods like start() and Stop() but don't know start and stop mechanism of every vehicle since they could have different start and stop mechanism e..g some can be started by kick or some can be by pressing buttons .



sources: 
http://javarevisited.blogspot.com/2010/10/abstraction-in-java.html#ixzz2dPnl6Rjb
http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
http://www.javatpoint.com/abstract-class-in-java

No comments:

Post a Comment