JAVA ABSTRACTION

The major use of abstract classes and methods is to achieve abstraction in Java.

Abstraction is an important concept of object-oriented programming that allows us to hide unnecessary details and only show the needed information.

This allows us to manage complexity by omitting or hiding details with a simpler, higher-level idea.

A practical example of abstraction can be motorbike brakes. We know what brake does. When we apply the brake, the motorbike will stop. However, the working of the brake is kept hidden from us.

The major advantage of hiding the working of the brake is that now the manufacturer can implement brake differently for different motorbikes, however, what brake does will be the same.

NOTE: Data abstraction is the process of hiding certain details and showing only essential information to the user.

The abstract keyword is a non-access modifier, used for classes and methods:

  • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
  • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

NOTE: In simple terms, abstraction “displays” only the relevant attributes of objects and “hides” the unnecessary details.

The general declaration of the abstract method is:

abstract void methodName (parameter_list);

While writing the abstract method, we need to remember the following rules:

  • A class containing one or more abstract methods is an abstract class.
  • Certain other keywords should not be used with the abstract keyword.

SAMPLE PROGRAM:

public abstract class VM
{
public void enjoy()
{
System.out.println(“Staying in UK”);
}
public abstract void repaydebts();
}

public class VMChild extends VM
{
public static void main(String[]args)
{
VMChild vmc = new VMChild();
vmc.enjoy();
vmc.repaydebts();
//VM vm = new VM();
//vm.enjoy();
//vm.repaydebts();
}
public void repaydebts()
{
System.out.println(“Selling Properties”);
}
//public abstract void casePending();

}

OUTPUT:

  • c:\java>javac VMChild.java
  • c:\java>java VMChild
    Staying in UK
    Selling Properties

Leave a comment

Design a site like this with WordPress.com
Get started