Abstraction In Java With Real Time Example With Diagram

Abstraction is one of the basic fundamental concepts of OOP’s.  Abstraction focus on functionalities not that how does it works. Some more points are listed below,

Concepts of Java, Abstraction, Abstraction with diagram, Abstraction with real time example, Java, java concept, OOPs concept, OOPs concept of java
Abstraction
  • Abstraction is process of hiding the internal implementation and showing only necessary functionality/features to the user.
  • Abstraction is implemented using interface and abstract class.

Important points on Abstraction

What is abstraction?

The process of hiding the internal implementation details or hide complexity and showing only features to the user.

Example

  • ATM machine: Here we can withdraw money using shown features but internal details or implementation details are hidden which are not really required to the user.
  • TV remote: Person who using TV remote can able to operate. But don’t know how it works internally.

How can we achieve abstraction?

Partial abstraction can be achieved by Abstract classes and 100% abstraction can be achieved by Interface.

What are the differences between Abstraction and Encapsulation?

In higher end both abstraction and Encapsulation looks similar. But purpose of these two concepts is totally different. Some of the differences are listed below,

  • Abstraction hides the things at design level but Encapsulation hide things at implementation level.
  • Abstraction can be achieved using interface and abstract class while Encapsulation can be achieved by using access modifiers e.g. public, private and protected.
  • Abstraction focus on What should be done and Encapsulation focus on How it should be done.

Example1

abstract class Test  // Abstract Class
{	
	final float pi=3.147f;
	static int age =25;
	String name = "Ramesh";
	
	public abstract void display();  // Abstract method
	
	public void show(){
		System.out.println("Show method");
	}	
}

class Display extends Test{	
	public void display() 
	{  
		//Abstract Method Body	
		System.out.println("** Inside display method **");
	}	
}

public class AbstractClass {	
	static int a =23;
	private String name;
	public String value;

	public static void main(String[] args) {
		Display dObj = new Display();	
		dObj.display();
	}
}

Output:
** Inside display method **

Example2

interface int1{	
	int a = 12;
	public void display();
}

interface int2{
	int b=13;
	public void display();	
}

interface int3 extends int1, int2{
	int c=15;
	public void display();	
}

public class Interface implements int3{	
	public static void main(String[] args) {
		Interface obj = new Interface();		
		obj.display();		
	}

	public void display() {
		System.out.println(" Implemented display body ");		
	}	
}

Recommended post in OOPs concept:

Leave a Comment

Your email address will not be published.