Java Abstraction

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Abstraction  If you inherit an abstract class, you have to

provide implementations to all the abstract


Abstraction is a process of hiding the implementation methods in it.
details from the user and providing only necessary
functionality is called abstraction. Abstract Method in Java:
The main purpose of abstraction is to represent the
essential features without including the background A method that is declared with abstract modifier in an
details. abstract class and has no implementation (means no
Realtime Examples of Abstraction in Java body) is called abstract method in Java. It does not
1.We all use an ATM machine for cash withdrawal, contain any body.
money transfer, retrieve min-statement, etc. in our
Syntax:
daily life.
But we don’t know internally what things are happening abstract type methodName(arguments); // No body
inside ATM machine when you insert an ATM card for
performing any kind of operation. For example:
2. A car owner knows how to drive it. He knows about
abstract void msg(); // No body.
various components of car and how to use them.
For example, a car owner knows that the accelerator
pedal is used to increase the speed of car, and pressing
the brake pedal stops it. To perform these simple
actions, you only need to know how to use these
components but not need to know how they function.
3. When you need to send SMS from your mobile, you
only type the text and send the message. But you don’t
know the internal processing of the message delivery.

How to Achieve Abstraction in Java?


There are two ways to achieve or implement
abstraction in Java program. They are as follows:
1. Abstract class (0 to 100%) Example:
2. Interface (100%)
public abstract class MyTest
Java Abstract Classes {
A Java class which contains the abstract keyword in its abstract void calculate(int a, int b); // No body.
declaration is known as abstract class. }
 Java abstract classes may or may not public class Addition extends MyTest
contain abstract methods, i.e., methods without {
body ( public void get(); ) void calculate(int a, int b)
 But, if a class has at least one abstract method, {
then the class must be declared abstract. int x = a + b;
 If a class is declared abstract, it cannot be System.out.println(“Sum: ” +x);
instantiated. }
 To use an abstract class, you have to inherit it }
from another class, provide implementations to public class Subtraction extends MyTest
the abstract methods in it. {
void calculate(int a, int b)
{
int y = a - b;
System.out.println(“Subtract: ” +y);
}
}
public class Multiplication extends MyTest
{
void calculate(int a, int b)
{
int z = a * b;
System.out.println(“Multiply: ” +z);
}
}
public class MyClass {
public static void main(String[] args)
{
// Creating objects of classes.
Addition a = new Addition();
Subtraction s = new Subtraction();
Multiplication m = new Multiplication();

// Calling methods by passing argument values.


a.calculate(20, 30);
s.calculate(10, 5);
m.calculate(10, 20);
}
}
Output:
Sum: 50
Subtract: 5
Multiply: 200

You might also like