Abstraction and its types in Java

Abstraction is the process of hiding the implementation details of a class or method and showing only the functionality to the user.

Note: Although we cannot create objects for abstract classes, we can extend abstract classes using a non abstract class and create objects for the non abstract class.

So…what are the ways to achieve abstraction in Java??

In Java we can achieve abstraction in any of the 2 ways:

  • Abstract Classes(0-100% Abstraction): Using abstract classes, we can achieve partial or complete abstraction.
  • Interface(100% Abstraction): Interfaces provide complete abstraction.

Complete Abstraction using Abstract class

Take a look at the below code block to understand the syntax for achieving complete abstraction using abstract class.

abstract class CompleteAbstractClass{
	//"abstract" keyword specifies that the class is an abstract class
	public abstract void methodName();
	//if we want to achieve 100% abstraction using abstract classes, 
	//all the methods defined in the abstract class should be abstract methods
}
class ChildClass extends CompleteAbstractClass{//inheriting an abstract class

	@Override
	public void methodName() {
		//implementation for abstract methods

	}
	/* Note:
	 * while extending an abstract class 
	 * we need to either implement all it's unimplemented methods 
	 * or else define the current class as abstract 
	 * */
}

public class Abstraction {//driver class

	public static void main(String[] args) {
		ChildClass obj1=new ChildClass();//creating an object of child class of the 
                                                 //CompleteAbstractClass
		obj1.methodName();

	}

}

Partial Abstraction using Abstract class

Take a look at the below code block to understand the syntax for achieving partial abstraction using abstract class.


abstract class PartialAbstractClass{
	//"abstract" keyword specifies that the class is an abstract class
	public abstract void AbstractMethodName();//abstract method
	
	public void regularMethodName() {//non abstract method
		//some implementation code
	}
	/*
	 * We may have some abstract methods and some non abstract methods in our abstract class.
	 * This is how we achieve partial abstraction
	 * */
	
}
class ChildClass extends PartialAbstractClass{//inheriting an abstract class

	@Override
	public void AbstractMethodName() {
		//implementation for abstract methods

	}
	/* Note:
	 * We may or may not override non abstract methods of abstract class as per our requirement.
	 * */
}

public class Abstraction {//driver class

	public static void main(String[] args) {
		ChildClass obj1=new ChildClass();//creating an object of child class of the 
                                                 //CompleteAbstractClass
		obj1.AbstractMethodName();//calling abstract method of abstract class which was overridden by 
                                          //child class
		obj1.regularMethodName();//calling non abstract method of abstract class using child class 
                                         //object

	}

}

Complete Abstraction using Interfaces

Interfaces allow us to achieve only complete abstraction as abstract methods cannot be defined in interfaces. We use the “implements” keyword to inherit interfaces to a non-abstract or abstract class.

In interfaces,

  • methods are by default public and abstract
  • variables are by default public, static and final

Take a look at the below code block to understand the syntax for achieving complete abstraction using the interface.

package corejava;

interface MyInterface{//an interface is defined using the keyword "interface"
	public void methodName();//methods in interface are by default public and abstract
}
class DerivedClass implements MyInterface{//interface can be inherited using the keyword "implements"

	@Override
	public void methodName() {
		//method body

	}

}
public class InterfaceExample {//Driver class

	public static void main(String[] args) {
		DerivedClass obj1=new DerivedClass();//creating an object of derived class
		obj1.methodName();

	}

}


Wrapping it up…

In this blog, we have discussed Abstraction in Java which is one of the most important principles of Object Oriented Programming. But as we know Java is vast and its vastness can sometimes be scary to new learners.
But not to worry as I’ll be covering most of the important topics that you can face while developing a website.
If your goal is to get a professional certification for Java you can refer to this course by Edureka and for an overall understanding of Java web development, you can refer to this course by Udacity.

Related Topics