Data abstraction in Java3 min read

What is Data Abstraction in Java?

Data abstraction in Java refers to the ability to make a class abstract in Object-Oriented Programming. The practice of identifying only the required attributes of an object while discarding the irrelevant information is known as data abstraction in Java. The features and behaviors of an object assist to distinguish it from other things of the same sort, as well as classify and group them.

  • The term “abstract class” refers to a class that cannot be instantiated. In short, there is no way to make an instance of the abstract class.
  • All of the class’s other features are still available, and its fields, methods, and constructors can all be accessed in the same way.
  • If a class is abstract and cannot be instantiated, it is of limited utility unless it is a subclass.
  • The common functionality of a collection of child classes is contained in a parent class, but the parent class is too abstract to be used on its own.
  • An abstract class can have both abstract and non-abstract methods.

Abstract Methods

  • If you want a class to have a method, but you want child classes to determine how that method is implemented, you can declare the method in the parent class as abstract.
  • The abstract keyword is also used to declare a method as abstract. An abstract method consists of a method signature, but no method body.
  • The abstract method would have no definition, and its signature is followed by a semicolon, not curly braces as follows:
public abstract class Employee{
  private String name;
  private String address;
  private int number;
  public abstract double computePay(); //abstract method

Declaring a method as abstract has three results:

  1. If a class has an abstract method, the class must also be abstract.
  2. Any child class must either override or declare itself abstract in order to use the abstract method.
  3. An abstract method must be overridden by a child class that inherits it. If they don’t, they’ll be abstract, and their child class will have to override it. A descendant class must eventually implement the abstract method; otherwise, you’ll wind up with a hierarchy of abstract classes that aren’t instantiable.

However, an interface is different from a class in several ways, including:

  1. An interface cannot be instantiated.
  2. There are no constructors in an interface.
  3. In an interface, all of the methods are abstract.
  4. Instance fields are not allowed in an interface. The only fields that can be used in an interface must be static or final.
  5. Multiple interfaces can be extended by a single interface.
  6. A class does not extend an interface; instead, it implements it.
    • Example: public class ChildClass implements ParentInterface{}

Abstract classes and methods in a nutshell

  • In some cases, we must build a superclass that declares the structure of abstraction without implementing all of its methods.
  • A class with one or more abstract methods must be declared abstract as well.
  • Subclasses can override some methods by specifying the abstract modifier. Because they are not implemented by a superclass, these methods are referred to as subclasses’ responsibility.
  • The new operator cannot be used to explicitly instantiate an abstract class. We can’t make abstract class objects, but we can make abstract class object references.


Leave a Reply