Object oriented GET 211
Object oriented GET 211
Key Concepts:
Encapsulation
Abstraction
Inheritance
Polymorphism
Encapsulation:
o Encapsulation is the bundling of data (attributes) and methods within a class and
restricting direct access to some of these elements. This is achieved by defining
methods to access or update the data (getters and setters).
o Example: In a "BankAccount" class, we might encapsulate the balance attribute
and provide methods to deposit and withdraw, preventing unauthorized direct
access to the balance.
Abstraction:
o Abstraction simplifies complex reality by modeling classes appropriate to the
problem and providing only necessary details.
o In OOP, abstraction is achieved through abstract classes and interfaces, which
define methods without implementing them.
o Example: In a vehicle system, an abstract "Vehicle" class could define a move()
method without specifying the details of how each specific type of vehicle moves.
Inheritance:
o Inheritance allows a new class to inherit attributes and methods from an existing
class, promoting code reuse.
o A subclass (derived class) inherits from a superclass (base class) and can have its
own additional attributes or behaviors.
o Example: A "Truck" class can inherit from a "Vehicle" class and add specific
attributes or methods unique to trucks, such as loadCapacity.
Polymorphism:
o Polymorphism allows objects of different classes to be treated as objects of a
common superclass. This enables one interface to be used for different underlying
forms (data types).
o Polymorphism is often achieved through method overriding and overloading.
o Example: Different subclasses of "Animal" (like "Dog" and "Cat") may have
their own sound() methods, but calling sound() on any "Animal" object will
produce an appropriate response.