Abstraction in Java
Abstraction in Java
Abstraction in
Java
Abstraction is a process of hiding
the implementation details and
showing only functionality to the
user.
Abstraction shows only essential things to
the user and hides the internal details,
for example, sending SMS where you type the
text and send
the message.
You don't know the internal processing about the
message delivery.
◼ Abstraction lets you focus on what the object does instead
of how it does it.
have a body.
The body is provided by the subclass (inherited from).
Example
//abstract class
abstract class Car{
abstract void accelerate();
}
//concrete class
class Suzuki extends Car{
void accelerate(){
System.out.println("Suzuki::accelerate");
}
}
class Main{
public static void main(String args[]){
Car obj = new Suzuki(); //Car object =>contents of Suzuki
obj.accelerate(); //call the method
}
}
The simple abstraction example that is given above has a class Car. In this class
Car, we have an abstract method to accelerate (). Then we inherit this class in
the Suzuki class. Inside the Suzuki class, we implement the accelerated method.
The above example simply shows the way in which an abstract class is defined,
inherited, and then used in the program.
Abstract Methods and
Classes
An abstract class is a class that is
declared abstract—it may or may not
include abstract methods.
Abstract classes cannot be instantiated, but
they can be subclassed (can be inherited).
An abstract method is a method that is
declared without an implementation
(without braces, and followed by a
semicolon), like this:
• Then in the main method, we create a bank object. First, the bank
object contains an object of ICICI class and displays the interest
rate. Next, the BOI object is created and it displays the interest
rate.