Abstraction In Java

We are explaining Java Abstract with examples, in the hopes that this would meet the prerequisites for learning.

The process of abstraction in java involves hiding the implementation details from the user and exposing only the functionality.

What Is Abstraction In Java

In Java, abstraction is a fundamental concept in object-oriented programming that allows you to create simplified and generalized representations of real-world objects. It focuses on showing only the essential features and behaviors of an object while hiding the unnecessary details. Abstraction helps in building complex systems by breaking them down into manageable and understandable components.

In Java, abstraction is achieved through two main mechanisms:

  1. Abstract Classes: An abstract class is a class that cannot be instantiated on its own but can be used as a blueprint for other classes. It may contain abstract methods (methods without a body) and concrete methods (methods with implementation). Subclasses (classes that extend the abstract class) must provide implementations for the abstract methods. Abstract classes allow you to define common behavior and attributes for a group of related classes.

Interfaces: An interface in Java is a collection of abstract methods that define a contract for classes implementing the interface. Unlike abstract classes, interfaces can only contain method signatures without any implementation. Classes can implement multiple interfaces, allowing them to inherit behavior from multiple sources.

By using abstraction, you can create hierarchies of classes and organize your code in a more maintainable and scalable way. Clients of the abstracted classes or interfaces can interact with the objects at a higher level without being concerned about their underlying complexities, making the code easier to understand and modify.

Java Abstract Classes and Methods

Data abstraction, as it relates to Java Abstract, is the practice of hiding some details and providing the user with only the most important information.

Abstract classes or interfaces can be used to accomplish abstraction (which you will learn more about in the next chapter).

A non-access modifier for classes and methods is the term abstract:

  • A restricted class is called an abstract when the object creation of the class is not possible (to access it, it should be extended by another class).
  • Only applicable in an abstract class, an abstract method lacks a body. The subclass contains the body of the abstract method (which is extended from the abstract class).

Both abstract and regular methods may be present in a java abstract class:

Example: 

abstract class Bank{void bank_name(){ System.out.println("The Bank name is Industrial & Commercial Bank of China Limited"); } abstract void bank_employees();}

It is impossible to make an object of the Bank class from the example given above:

Bank my_object=new Bank();

It should inherit from another class in order to have access to the abstract class. Let’s transform the class we used in the Polymorphism chapter into a Java abstract class.

As you may recall from the chapter on inheritance, we utilize the extends keyword to inherit from a class.

Example: 

Another Example: 

abstract class Student{void student_name(String mrx){ System.out.println("Student Name: "+mrx); }abstract void student_id(int id);}class Student_Info extends Student{void student_id(int id){ System.out.println("Student ID number: "+id); } }public class Main { public static void main(String[] args) {Student_Info my_obj=new Student_Info(); my_obj.student_name("Charles"); my_obj.student_id(1245134); }// Output 1: Student Name: Charles // Output 2: Student ID number: 1245134 }


Java Abstraction Key Points

  1. The abstract keyword must be used when declaring an abstract class.
  2. Methods can be abstract or non-abstract.
  3. There is no way to instantiate it.
  4. Static methods and constructors can also be included.

 

Use Abstract Classes and Methods: When and Why?

Hide essential data and only display key information about an object to establish security.

Notably, interfaces—about which you will learn more in the following chapter—can also be used to achieve abstraction.

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *