0% found this document useful (0 votes)
3 views

Abstraction in Java

Abstraction in Java is a process of hiding implementation details while exposing only essential functionality to the user, implemented through abstract classes and interfaces. An abstract class cannot be instantiated and may contain abstract methods without implementation, while an interface provides complete abstraction with only method prototypes. This concept allows developers to focus on what an object does rather than how it does it, facilitating better design and code organization.

Uploaded by

kookiex143
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Abstraction in Java

Abstraction in Java is a process of hiding implementation details while exposing only essential functionality to the user, implemented through abstract classes and interfaces. An abstract class cannot be instantiated and may contain abstract methods without implementation, while an interface provides complete abstraction with only method prototypes. This concept allows developers to focus on what an object does rather than how it does it, facilitating better design and code organization.

Uploaded by

kookiex143
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Abstraction in OOP

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.

Department of Computer Science 2


How Abstraction is
implemented in
 There are two ways to achieve Java
abstraction in java
 Abstract class
 Interface

• Java provides a non-access modifier “abstract” for


implementing abstraction. This abstract modifier can be used
with classes and methods but not variables.

• The interface provides complete abstraction i.e. it only


provides method prototypes and not their implementation. An
abstract class provides partial abstraction wherein at least
one method should not be implemented.

Department of Computer Science 3


 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).
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:

Department of Computer Science 6


Cont

 Points to remember,
 An abstract class must be declared with an
abstract keyword.
 It can have abstract and non-abstract
methods.
 It cannot be instantiated.
 It can have constructors and static methods
also.
 It can have final methods which will force
the subclass not to change the body of
the method.
 When an abstract class is subclassed, the
subclass usually provides implementations
Department of Computer Science 7
for all of
Abstract
Method
 A method which is declared as abstract
and does not have implementation is
known as an abstract method.
 For example
 abstract void printStatus(); //No method body

Department of Computer Science 8


Example
Program
 In this example, Bike is an abstract
class that contains only one abstract
method run.
 Itsimplementation is provided by the
Honda class.

Department of Computer Science 9


// Abstract
abstract
class class
Progra
Animal {
// Abstract method (does not have a
body) public abstract void m
animalSound();
// Regular
method public
void sleep() {
System.out.prin The horse says: hee
} tln("Zzz"); hee
Zz
} z
// Subclass (inherit from
Animal) class horse extends
Animal {
public void animalSound()
{
// The body of
animalSound() is provided
here
System.out.println("The
horse says: hee hee");
}
}

public class MyMainClass {


public static void
main(String[] args) {
//save
Department of Computer 2
horse myHorse = new
MyMainClass.java
Science 2
Let’s implement an example of an abstract class and
an abstract method.
Output
Explanation

• In the above example, we have a Bank class. In this class, we


have an abstract method, getInterestRate (). Then we declare two
classes – ICICI and BOI that inherit from the Bank class. Both these
classes implement the getInterestRate () method by returning
their respective interest rates.

• 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.

• Thus, we can assume that the Bank class is a sort of a sketch or a


structure that allows us to get an interest rate. From this
structure, we can create as many concrete classes as we want,
and then we can get the respective interest rates for each bank
object (this is shown in the main method).
Use Of An Abstract Class In Java
Why do we use an abstract class when in reality it does
not have any implementation of its own?

Along with the answer to the above question, we will also


illustrate how to use an abstract class in the example that follows.

Let’s consider an example of Vehicles. We know that Vehicles can


be of many types. We can have Cars, Scooters, bikes, mopeds,
buses, etc. Though there are many types of vehicles, they have
some properties or attributes that are common to all vehicles
irrespective of their types.

For example, each vehicle has a model, chassis number, color,


etc. Each of them has functions like start, stop, accelerate, brake,
etc. Now each vehicle will have the above properties and
methods that are common to the others as well. At the same time
as to vehicle users, we might not be interested in some aspects.
• For example, if a person is driving a car, what he/she will be
interested in is just to start and stop the vehicle or accelerate or
brake the vehicle. He/she will not be interested in knowing how
the vehicle starts or stop. We are only interested in the abstract
working of the functions and not in their details.

• Now if we want to represent the above example system into a


software application, then how do we design it? First of all, we will
implement some abstractions. Now, we know that some functions
are common but depending on each model the implementation of
these functions will be different.

• To begin with, we declare an abstract class “Vehicle”.


Explanation
• So we will have a Vehicle abstract class and three classes Car,
Bike, and Scooter. Each of these classes will extend the Vehicle
class and override each of the abstract methods.

• Thus in general, whenever we have to represent such a system


that has common methods or operations to represent, then to
present only the outer perspective to the user, we go for
abstraction. As a result, we take out the common methods and
represent them as abstract methods and collect these abstract
methods in a common abstract class.

• Once we have the outline of a system represented as an abstract


class and the operations as abstract methods, we can then derive
any number of classes from the given abstract class and override
the abstract methods to implement these operations for each
class.

• This way it becomes useful to design a system.

You might also like