Tuesday, 7 April 2015

Abstraction in Java

Abstraction is one of the four fundamental OOP concepts. The other three being -

Abstraction means hiding the complexity and only showing the essential features of the object. So in a way, Abstraction means abstracting/hiding the real working and we, as a user, knowing only how to use.
Real world example would be a vehicle which we drive with out caring or knowing what all is going underneath.
A TV set where we enjoy programs with out knowing the inner details of how TV works.
An example from Java would be - Java Database Connectivity (JDBC) API which provides universal data access from the Java programming language. Using the JDBC API, we can access virtually any data source without knowing how the driver for that particular data source is implemented. All we have is an API with a given set of methods.

Abstraction in Java

Abstraction in Java is achieved through -

  • Interface - Which defines an expected behaviour, with out providing any details about the behavior.
  • Abstract Class - Which provides incomplete functionality and leave it on the extending class to fill the gaps.

Abstraction example through Interface

public interface MyInterface {
 void method1();
 String method2(String Id);
}

As it can be seen there are 2 methods method1 and method2. Where method1() neither take any parameter nor return any value. Whereas, method2() takes String parameter and returns a String. The user's who use this interface just know the method and how to call them, how these methods are implemented is abstracted from the users.

Points to note -

  • Abstraction is one of the four fundamental OOPS concept. Other being, inheritance, polymorphism and encapsulation.
  • Abstraction is a way to hide the real implementation and user just knows how to use it.
  • In Java abstraction is achieved through interfaces and abstract classes.

That's all for this topic Abstraction in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Method overloading in Java
  2. Method overriding in Java
  3. Class in Java
  4. abstract class in Java
  5. Difference between Encapsulation and Abstraction in Java
  6. Java OOP interview questions

You may also like -

>>>Go to Java Basics page

2 comments: