Java Abstraction

java-Logo
Java Tutorials
Java Environment
Environment Setup
Declaration & Assignments
Flow Control
Miscellaneous
Java OOPs

Data Abstraction is technique whose feature provides us the capability of differentiating essential details that need to be displayed to the user.Stuff that should remain hidden or unfamiliar to users or those data that acts as non-essentials units can be put away from displaying to users. For example, A building can be viewed as a cement constructed building rather than separate components like rods, cement, bricks etc.

Again abstraction of data can also be termed as the procedure of picking up the only the necessary characteristics, ignoring the unrelated or extraneous details. The characteristics and actions of an object distinguish it from other objects which are alike and leads to classification or grouping the objects.

Likewise in Object-oriented programming, abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.

Java provides the concept of abstraction through Abstract classes and interfaces. A class containing the keyword abstract in its declaration creates the abstract class. It may or may not contain any abstract methods within it. When a class is classified as abstract, it cannot be instantiated. For using a class as abstract, it needs to be inherited from another class which has the abstract method implementation. It is to be noted that if a class has a minimum of one method as abstract, then the class has to be declared as abstract.

Example:
abstract class cycle {
 abstract void work();
}

class HeroCycle extends cycle {
 void work() {
  System.out.println("Selling good");
 }
 
 public static void main(String argu[]) {
  cycle o = new HeroCycle();
  o.work();
  System.out.println("Code executed");
 }
 
}

Abstract Methods

When a programmer wants any class having a specific method but also want the actual execution of that method to be resolute by its child classes, the methods can be declared in the base class in the form of abstract methods.

  • The keyword abstract is used for declaring abstract method
  • The abstract keyword needs to be implemented before the name of method
  • Abstract method does not contain any method body
  • If there are no curly braces given, there should have to be semicolon put at the end of the abstract method

Program for Abstract Method

This is how abstract methods get declared. You have to write the full functionality of this method in another non-abstract class, taking the same method name.

Example:

public abstract class professor

public abstract class professor {
 private String str;
 private float sal;
 private int regnNo;
 public abstract double salaryCompute();
 // abstract metho body here
}

Scroll Back to Top