This tutorial talks about Understating of Abstraction in Java.This is one of the important Oops feature in object oriented programming language.
Abstraction is one of the four fundamental Oops concepts. The other three are-
Abstraction is a concept or an Idea which is not associated with any particular object or instance. Using abstract classes/
interfaces we can express the intent of the class rather than the actual implementation. In a way, one class should
not know the inner details or implementation of another in order to use it, just knowing the interfaces should be good more than enough.
We can take some real world example of abstraction like–
A vehicle which we drive without knowing what all is going underneath.
A TV set where we enjoy programs without knowing the inner details of how TV works.
An example of abstraction in Java would be
Java Database Connectivity (JDBC) API which provides universal database 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. we have only an API with a given set of methods
Abstraction in Java
Abstraction in Java we can achieve in two ways –
- Interface– Which defines an expected behavior without providing any details about the behavior.
- Abstract Class– Which provides incomplete functionality and leave it on the extending class to fills the gaps.
Let’s try to understand how we can achieve Abstraction in Java through Interface
We have an interface MyInterface.java
1 2 3 4 5 6 |
package com.infotech.client1; public interface MyInterface { public abstract void printWelcomeMessage(String message); public abstract String getWelcomeMessage(String message); } |
You can see here, there are 2 methods in the interface MyInterface
printWelcomeMessage and getWelcomeMessage.
Here printWelcomeMessage method has one parameter of type String and does not return any value. Whereas, getWelcomeMessage method has one String parameter and returns a String value.
Let’s say we have two classes MyClassImpl1.java and MyClassImpl2.java implementing the interface MyInterface.
MyClassImpl1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.infotech.client1; public class MyClassImpl1 implements MyInterface { @Override public void printWelcomeMessage(String msg) { System.out.println("Message is " + msg); } @Override public String getWelcomeMessage(String str) { return "Hello " + str; } } |
Here the printWelcomeMessage method is implemented to print the passed parameter. Method getWelcomeMessage prefixes Hello to the passed parameter.
MyClassImpl2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.infotech.client1; public class MyClassImpl2 implements MyInterface { @Override public void printWelcomeMessage(String msg) { System.out.println("Message in Uppercase " + msg.toUpperCase()); } @Override public String getWelcomeMessage(String str) { return str.toUpperCase(); } } |
Here the printWelcomeMessage method is implemented to print the passed parameter in upper case. Method getWelcomeMessage returns the passed parameter after converting it to upper case.
ClientTest.java
This client is going to use above created interface and both implementation classes to understand Abstraction in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.infotech.client1; public class ClientTest { public static void main(String[] args) { MyInterface obj; // Holding reference of class MyClssImpl1 obj = new MyClassImpl1(); obj.printWelcomeMessage("Calling class.."); String result1 = obj.getWelcomeMessage("OOps Concept.."); System.out.println("Value - " + result1); // Holding reference of class MyClssImpl2 obj = new MyClassImpl2(); obj.printWelcomeMessage("Calling class.."); String result2 = obj.getWelcomeMessage("OOps Concept.."); System.out.println("Value - " + result2); } } |
If you run ClientTest.java you will get below output:
1 2 3 4 |
Message is Calling class.. Value - Hello OOps Concept.. Message in Uppercase CALLING CLASS.. Value - OOPS CONCEPT.. |
In ClientTest class there is an object of type MyInterface when it has reference of type MyClassImpl1 it is calling methods of that class, when it has reference of type MyClassImpl2 it is calling methods of that class. But as a user you are abstracted from the different implementations.
Would you like to watch on YouTube:
That’s all about Understating of Abstraction in Java.
You May Also Like:
What are JVM, JRE and JDK in Java?
Differences between Java EE and Java SE
Encapsulation in Java
Understating of Polymorphism in Java
Method Overloading or Static Polymorphism in Java
Method Overriding or Dynamic Polymorphism in Java
Understating of Inheritance in Java
If you have any feedback or suggestion please feel free to drop in blow comment box.