• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Inheritance » What is Abstraction in Java? Abstract Class or Interface
Next →
← Prev

What is Abstraction in Java? Abstract Class or Interface

By Dinesh Thakur

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.

We’ll be covering the following topics in this tutorial:

  • Abstract classes and Abstract methods :
  • Extending Abstract Classes  
  • Abstraction Syntax  
  • Why Abstraction is Important
  • Abstract Class vs Interface  

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:

import java.io.Console;
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.

You’ll also like:

  1. Difference between abstract class and interface in java
  2. What is Abstract Class in Java?
  3. Write a C++ program illustrates Abstract class and Pure virtual function.
  4. What is Abstraction? Explain Type of Abstraction.
  5. Abstract Methods and Classes in Java Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW