Junior — Middle
Can you explain what is meant by abstraction in object-oriented programming?
sobes.tech AI
Answer from AI
Abstraction in object-oriented programming is a principle that allows highlighting only the important characteristics of an object, hiding implementation details. This helps focus on what the object does, rather than how it does it.
For example, a "Car" class might have methods start(), drive(), brake(), while the internal mechanisms of the engine or transmission are hidden from the class user.
In Python, abstraction is achieved by creating classes with methods that describe behavior, and implementation details can be hidden inside methods or through the use of abstract base classes (module abc).
Example:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def make_sound(self):
pass
class Dog(Animal):
def make_sound(self):
print("Woof")
# Usage
my_dog = Dog()
my_dog.make_sound() # Outputs: Woof
Here, Animal defines an abstract interface, and concrete classes implement the details.