0% found this document useful (0 votes)
19 views2 pages

Abstraction in Object-Oriented Prog

Uploaded by

aduragbemihajoh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Abstraction in Object-Oriented Prog

Uploaded by

aduragbemihajoh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Abstraction in Object-Oriented Programming (OOP) focuses on hiding the complex

details of implementation and exposing only the essential features of an object or


class. It enables the creation of abstract concepts in code, allowing developers to
focus on what an object does rather than how it does it.

Types of Abstraction in OOP


Abstraction in OOP can be categorized based on its implementation:

1. Data Abstraction
Refers to exposing only relevant attributes and methods of an object while hiding
unnecessary details.
Achieved using access modifiers like public, private, and protected to restrict
access to certain class members.
For example, consider a Car class:
java
Copy code
class Car {
private String engine; // Hidden (implementation detail)
public void startEngine() { // Exposed (essential functionality)
System.out.println("Engine started");
}
}

public class Main {


public static void main(String[] args) {
Car myCar = new Car();
myCar.startEngine(); // Can use the method but cannot access engine
directly
}
}
2. Functional Abstraction
Involves hiding the details of how a function works and exposing only its purpose
or result.
Methods are abstract in the sense that users of the function care only about its
behavior, not its internal logic.
3. Class-Level Abstraction
Achieved through abstract classes and interfaces, which provide a blueprint for
other classes without implementing the details.
Subclasses or implementing classes define the details for abstract methods.
Abstract Classes
An abstract class is a class that cannot be instantiated and may contain both
implemented (concrete) and unimplemented (abstract) methods.
Example:
java
Copy code
abstract class Animal {
abstract void sound(); // Abstract method (no implementation)
void sleep() { // Concrete method (has implementation)
System.out.println("Sleeping...");
}
}

class Dog extends Animal {


@Override
void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.sound(); // Output: Bark
myDog.sleep(); // Output: Sleeping...
}
}
Interfaces
An interface defines a contract that implementing classes must fulfill by providing
concrete implementations for all its methods.
In modern languages, interfaces may also include default methods with
implementations.
Example:
java
Copy code
interface Animal {
void sound(); // Abstract method
}

class Cat implements Animal {


@Override
public void sound() {
System.out.println("Meow");
}
}

public class Main {


public static void main(String[] args) {
Animal myCat = new Cat();
myCat.sound(); // Output: Meow
}
}
Key Differences Between Abstract Classes and Interfaces:
Feature Abstract Classes Interfaces
Inheritance A class can inherit only one abstract class. A class can implement
multiple interfaces.
Method Types Can have both abstract and concrete methods. Can only have
abstract methods (until Java 8, which introduced default methods).
Usage Suitable for shared code and abstraction. Suitable for defining a contract
that multiple classes can implement.
Benefits of Abstraction:
Simplifies complex systems by exposing only essential features.
Promotes code reusability and flexibility.
Enhances maintainability by decoupling implementation details from usage.

You might also like