0% found this document useful (0 votes)
56 views8 pages

Oop Finals Reviewer

The document provides an overview of key concepts in Object-Oriented Programming (OOP) including encapsulation, inheritance, polymorphism, and abstraction. It defines each concept and provides a code example to illustrate how it is implemented in C++. Encapsulation is demonstrated using a Car class that bundles data and methods together. Inheritance is shown via a Mammal class extending an Animal class. Polymorphism is exhibited through shape classes overriding a draw method. Abstraction is portrayed using a RemoteControl class declaring an abstract method.

Uploaded by

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

Oop Finals Reviewer

The document provides an overview of key concepts in Object-Oriented Programming (OOP) including encapsulation, inheritance, polymorphism, and abstraction. It defines each concept and provides a code example to illustrate how it is implemented in C++. Encapsulation is demonstrated using a Car class that bundles data and methods together. Inheritance is shown via a Mammal class extending an Animal class. Polymorphism is exhibited through shape classes overriding a draw method. Abstraction is portrayed using a RemoteControl class declaring an abstract method.

Uploaded by

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

OOP FINALS REVIEWER

Object-Oriented Programming (OOP) is a programming paradigm that organizes code into


objects, which are instances of classes. These objects can have attributes (data) and methods
(functions) that operate on the data. OOP promotes the concepts of encapsulation, inheritance,
polymorphism, and abstraction.
Encapsulation
Encapsulation is the bundling of data (attributes) and the methods that operate on that data into
a single unit known as a class. It restricts access to some of an object's components and prevents
the direct modification of its internal state.
Consider a car as an object. The details of how the engine, transmission, and other components
work are encapsulated within the car. You interact with the car through well-defined interfaces
like the steering wheel, pedals, and dashboard.
Inheritance
Inheritance is a mechanism that allows a class (subclass/derived class) to inherit the properties
and behaviors of another class (superclass/base class). It promotes code reuse and the creation
of a hierarchy of classes.
In the animal kingdom, different species share common characteristics inherited from their
ancestors. For instance, mammals share common traits like having a spine, being warm-blooded,
and nursing their young. Here, mammals would be the subclass inheriting from the superclass
"animals."
Polymorphism
Polymorphism allows objects of different types to be treated as objects of a common type. It
enables the same operation to be performed on different types of objects, and the appropriate
method is invoked based on the object's type.
Think of a simple "Shape" class with a method called calculateArea(). Different subclasses like
"Circle" and "Square" can inherit from the "Shape" class and implement their own version of
calculateArea(). When you call calculateArea() on a shape object, polymorphism ensures the
correct method is called based on the actual type of the object.
Abstraction
Abstraction involves hiding the complex implementation details of an object and exposing only
the essential features. It allows developers to focus on what an object does rather than how it
achieves its functionality.
When you use a television remote, you don't need to know the inner workings of the TV. The
remote provides an abstract interface with buttons for basic operations like changing channels
and adjusting volume. The underlying complexity is abstracted away.
Encapsulation

In this example, the Car class encapsulates the details of the car (model and year). The private
attributes (model and year) can only be accessed and modified through public methods
(setDetails and displayDetails), providing a controlled interface to the outside world.
Inheratance

Here, the Mammal class inherits from the Animal class. It can use the eat() method from the
base class and also has its own method (giveBirth). Inheritance allows for code reuse and the
creation of a hierarchy.
Polymorphism

The provided C++ code illustrates the concept of polymorphism through a hierarchy of classes
representing different shapes. Three classes—Shape, Rectangle, and Circle—are defined, each
with a draw() method. The draw() method is overridden in the subclasses to provide specific
implementations. In the main function, objects of Circle and Rectangle are created, and despite
both objects having a draw() method, the actual behavior is determined by the type of the object
at runtime. This demonstrates the polymorphic nature of the code, where different objects can be
treated uniformly through a common interface, allowing for flexibility and code reuse.
Abstraction

The RemoteControl class declares an abstract method pressButton. The TVRemote class,
which inherits from RemoteControl, provides a concrete implementation. Users of
RemoteControl only need to know about the abstract method, abstracting away the details of
how each remote control works internally.
You should be familiar with the following codes to identify what specific property is
being applied.
1. Encapsulation

Key Codes: Private and Public Access Specifiers: private, public, protected
Getter and Setter Methods

2. Inheritance:

Key Codes:
class keyword
public, protected, private access specifiers
: public BaseClass (or protected, private) for inheritance

3. Polymorphism:

Key Codes:
virtual keyword for declaring virtual methods
override keyword for indicating overridden methods in derived classes

4. Abstraction:

Key Codes:
virtual methods in abstract classes
= 0 (pure virtual function) for creating abstract classes
Other terms you should be familiar with
Class: A blueprint or template for creating objects. It defines the properties (attributes) and
behaviors (methods) that objects created from the class will have.
Example: In C++, class MyClass { /* class members */ };
Object: An instance of a class. It is a concrete realization of the properties and behaviors
defined by a class.
Example: MyClass obj;
Method: A function associated with an object or a class. Methods define the behaviors that
objects of a class can exhibit.
Example: void myMethod() { /* method implementation */ }
Constructor: A special method that is automatically called when an object is created. It
initializes the object's state.
Example: In C++, MyClass() { /* constructor code */ }
Destructor: A special method that is automatically called when an object is destroyed. It is
responsible for releasing resources or performing cleanup.
Example: In C++, ~MyClass() { /* destructor code */ }
Attribute/Member Variable/Field: A data member of a class that represents the state or
characteristics of an object.
Example: int myAttribute;
Encapsulation: The bundling of data (attributes) and methods that operate on the data into a
single unit (class). It restricts direct access to some of an object's components.
Example: Private and public access specifiers in C++ classes.
Inheritance: The mechanism by which a new class (subclass/derived class) inherits properties
and behaviors from an existing class (superclass/base class). It promotes code reuse.
Example: class DerivedClass : public BaseClass { /* class members */ };
Polymorphism: The ability of a single function or method to operate on different types of
objects. It includes compile-time (overloading) and runtime (overriding) polymorphism.
Example: Overriding a virtual method in a derived class.
Abstraction: The process of hiding the complex implementation details of an object and
exposing only the essential features. It focuses on what an object does rather than how it
achieves its functionality.
Example: Declaring an abstract class or interface in C++.
Method Overloading: Defining multiple methods in the same class with the same name but
different parameter lists.
Example: void myMethod(int x); and void myMethod(double y);
Method Overriding: Providing a specific implementation for a method in a subclass that is
already defined in its superclass.
Example: Overriding the draw() method in a derived class.

You might also like