by Dinesh Thakur Category: Inheritance

Abstraction in Java: The words "data abstraction" and "information hiding" are generally used interchangeably. The two terms mean the same thing in this context. Abstraction is a simple representation of a complicated situation. It is a technique where we hide irrelevant details and represent only the essential aspects of a context so that one can focus on features one is interested; It helps to deal a complex system by concentrating on the essential features only. It is designed to make it easier to maintain, read and work on the code. In the object-oriented model, a class is an abstraction of existing entities in the domain of the software system. Ex: A car viewed as a car rather than its components.

Data Abstraction is one of the chief features of OOP that hides the implementation details from their specifications. It is the process of identifying specific characteristics of an object ignoring irrelevant information. In other words, it is a mixture of encapsulation and data hiding. It encapsulates all the essential aspects of the objects of a class. The attributes of an object are called data members, and the functions that operate on that data are called member functions or methods. Since Java operate with classes, that feature abstraction, so, the classes are also called abstract data types.

In java, data abstraction is carried out by interfaces and abstract classes. but you can achieve 100% abstraction using interfaces.

Abstract classes and Abstract methods :

Abstract methods are an essential concept in the Java programming language.
• A class can be declared abstract even if it does not have abstract methods.
• A class is defined abstractly by putting the keyword abstract in front of the class keyword in the first line of class definition.
• An abstract method with only method signature (for example return type, method name, list of parameters and optionally throws clause) without any implementation or a concrete body or no functionality. These methods are declared  with the abstract keyword in front of the method name.
• An abstract class is a collection of abstract and non-abstract methods, an abstract class can be entirely abstract, but there is
a limitation of a class, that more than one class can be inherited.
• Abstract classes are useful to define other classes that can be used to instantiate objects, but they cannot be instantiated using the new operator.
• If a subclass does not implement all the abstract methods that it inherits, the subclass must be specified as abstract.
• A class can have at least one abstract methods, that class must also declare as abstract. To declare a class as abstract, add abstract keyword before the class keyword such a class called an abstract class.
• Abstract classes can not instantiate. In other words, if a class is declared abstract, you can not create an object of this class.
• The use of an abstract class is to provide a superclass from which other classes can inherit interfaces and implementations. The classes from which you can create instances (objects) are called concrete classes.
The abstract keyword is the mirror opposite of the final keyword. When the final keyword used on a class definition, the class cannot extend. When the abstract keyword used on a class definition, the class must extend. That's why it is not possible to declare a class both abstract and final. When an abstract class is defined, a subclass of the abstract class must give a concrete method. Otherwise, the subclass also becomes an abstract class.

The following program has an abstract class and an abstract method:

abstractclass Shape {
     double area;
     finaldouble Pi =Math.PI;
     abstractdouble Area();// abstract method
class Circle extends Shape {
     double radius;
     Circle(double r){ radius = r;}
     double Area(){
        area = Pi * radius * radius;
        return area;
public class Abstraction {
         public static void main(String args[]){
              Console console = System.console();
              Circle circle = new Circle(5.0);
              double area;
              area = circle.Area();
              console.printf("Area of the circle = %5.3f%n", area);

Extending Abstract Classes  

So, an abstract class may have an abstract method with no implementation, and that means not being able to instantiate the abstract class. If we were to attempt to instantiate it, we would find that the object is next to useless because abstract methods have no implementation. To stop this happening, Java does not allow abstract class instantiation.  The primary activity to do is to extend the abstract class; here we can implement the abstract method and then the new class can instantiate. Here’s how to do it.  We define the abstract class with an abstract method:

abstractclass Sports{
   abstract football();//Method without a body
Next, we extend the abstract class and then implement the abstract methods:
class game extends Sports{
   void football(); {
         System.out.println("play Football");

Abstraction Syntax  

Abstraction syntax begins with the abstract keyword, followed by the name of the  class and then a combination of both abstract methods and non-abstract methods.  
abstract class <ClassName>{  
  //Combination of abstract methods without a body or any implementation and  non-abstract methods that do have a body and implementation.
  //If a class contains at least one abstract methods, it must be declared as an abstract class  

Why Abstraction is Important

Abstraction is important because:

• It hides complexity and shows functionality, making the system easier to maintain.
• It provides a great deal more power to OOP languages when used together  with other OOP concepts like polymorphism and encapsulation.  
• It helps provide real solutions to real problems.  

Abstract Class vs Interface  

• The abstract class and interface are fundamentally the same as in that they both have an abstract method and neither can instantiate. There are also differences between the two:  
• Abstract classes may extend just a single class at any one time whereas the interface may extend multiple interfaces at any one time.  
• An abstract class may have abstract and non-abstract classes whereas the interface may only have abstract methods.  
• The abstract keyword is utilized to declare an abstract method, but the keyword is optional in the interface because all methods in the interface are abstract.  
• The abstract class may contain public, private, and abstract public methods whereas interface may only contain abstract public methods.  
• The abstract class may contain final, static or static final variable using any access specifier whereas interface may only have final static variables. Before we move onto the next major OOP concept, we’ll take some time out to explore the interface and how it works with abstraction.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles