⬅ Previous Topic
Encapsulation in Programming - OOPNext Topic ⮕
Inheritance in Programming - OOP⬅ Previous Topic
Encapsulation in Programming - OOPNext Topic ⮕
Inheritance in Programming - OOPAbstraction is one of the core principles of Object-Oriented Programming (OOP). It means hiding complex implementation details and showing only the essential features of an object or process. The goal of abstraction is to reduce programming complexity and effort.
Think of a car. When you drive a car, you interact with the steering wheel, pedals, and buttons—but you don’t need to know how the engine works, how fuel is injected, or how the transmission is managed internally. That internal complexity is abstracted away from you.
Answer: Abstraction is about what an object does, while encapsulation is about how it does it. Abstraction hides the unnecessary details, and encapsulation wraps data and methods into a single unit.
Imagine a software that handles different types of payments—credit card, debit card, and digital wallets. Instead of writing separate code for each everywhere, you define a common abstract structure.
class PaymentMethod:
method processPayment(amount)
class CreditCard inherits PaymentMethod:
method processPayment(amount):
print "Processing credit card payment of", amount
class DigitalWallet inherits PaymentMethod:
method processPayment(amount):
print "Processing wallet payment of", amount
// Main program
payment = new CreditCard()
payment.processPayment(100)
Processing credit card payment of 100
PaymentMethod
defines a generic structure or contract with processPayment()
.CreditCard
and DigitalWallet
implement their own specific behavior.Answer: Yes! If a new payment type (e.g., Cryptocurrency) needs to be added, you can simply create a new class that inherits from PaymentMethod
and implement processPayment()
—no changes to existing code required.
Let’s say we’re building a system with different types of input devices: touch screen, mouse, and keyboard. Instead of building separate logic each time, we abstract the interaction.
class InputDevice:
method receiveInput()
class TouchScreen inherits InputDevice:
method receiveInput():
print "Touch input received"
class Mouse inherits InputDevice:
method receiveInput():
print "Mouse input received"
// Main program
device = new TouchScreen()
device.receiveInput()
Touch input received
InputDevice
defines the common interface.receiveInput()
method.Answer: Because abstraction enables loose coupling. The code using these classes doesn’t depend on specific implementations. This makes the code cleaner, easier to maintain, and extensible.
Answer: It results in an error or incomplete implementation, because abstraction requires derived classes to provide the implementation of declared abstract methods.
⬅ Previous Topic
Encapsulation in Programming - OOPNext Topic ⮕
Inheritance in Programming - OOPYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.