Docsity

Object Oriented Programming (OOP) Complete Notes | Classes, Inheritance, Polymorphism, Study Guides, Projects, Research of Computer Science

Partial preview of the text

Download Object Oriented Programming (OOP) Complete Notes | Classes, Inheritance, Polymorphism and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Object Oriented Programming

Complete Concepts Guide for Exams & Interviews

Covers Classes, Objects, Encapsulation, Inheritance, Polymorphism, Abstraction, Constructors, and Access

Modifiers โ€” with C++/Java-style examples and comparison tables.

Table of Contents

  • Public โ€” accessible from anywhere the object is visible. class BankAccount { private: double balance; // hidden from outside public: void deposit(double amt) { balance += amt; } double getBalance() { return balance; } }; Note: Encapsulation is why we use getters/setters instead of exposing variables directly โ€” it protects data integrity.
  1. Abstraction

Abstraction means showing only the necessary details to the user while hiding the internal implementation. For

example, when you drive a car, you use the steering wheel and pedals without needing to know the engine's

internal mechanics.

  • Achieved in OOP through abstract classes and interfaces.
  • An abstract class cannot be instantiated directly and may contain abstract (unimplemented) methods that derived classes must define. abstract class Shape { abstract double area(); // no implementation here } class Circle extends Shape { double radius; double area() { return 3.14 * radius * radius; } }
  1. Inheritance

Inheritance allows a class (child/derived class) to acquire the properties and behaviors of another class

(parent/base class), promoting code reuse and establishing an 'is-a' relationship.

5.1 Types of Inheritance

  • Single โ€” one child class inherits from one parent class.
  • Multilevel โ€” a class inherits from a class which itself inherits from another.
  • Multiple โ€” a class inherits from more than one parent class (supported in C++, not in Java directly).
  • Hierarchical โ€” multiple child classes inherit from a single parent class. class Animal { public: void eat() { cout << "Eating..."; } }; class Dog : public Animal { // Dog inherits from Animal public: void bark() { cout << "Barking..."; } };
  1. Polymorphism

Polymorphism means 'many forms' โ€” the ability of a function, method, or object to behave differently based on

context. It comes in two main types:

6.1 Compile-Time Polymorphism (Static Binding)

Achieved through method/function overloading โ€” multiple functions with the same name but different

parameters, resolved at compile time.

int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } 6.2 Run-Time Polymorphism (Dynamic Binding)

Achieved through method overriding โ€” a derived class provides its own implementation of a method already

defined in its parent class, resolved at runtime using virtual functions.

class Animal { public: virtual void sound() { cout << "Some sound"; } }; class Cat : public Animal { public: void sound() override { cout << "Meow"; } };

  1. Constructors & Destructors

A constructor is a special method automatically called when an object is created, typically used to initialize data

members. A destructor is called automatically when an object is destroyed, typically used to release resources.

7.1 Types of Constructors

  • Default constructor โ€” takes no parameters.
  • Parameterized constructor โ€” accepts arguments to initialize an object with specific values.
  • Copy constructor โ€” creates a new object as a copy of an existing object. class Point { public: int x, y; Point() : x(0), y(0) {} // default Point(int a, int b) : x(a), y(b) {} // parameterized ~Point() { cout << "Destroyed"; } // destructor };
  1. OOP vs Procedural Programming Aspect OOP Procedural Approach Object/data-centric Function/procedure-centric Data Security Encapsulation hides data Data is globally accessible Code Reuse High, via inheritance Low, via function calls only Real-world Modeling Natural (objects = entities) Less intuitive Examples C++, Java, Python C, Pascal
  2. Quick Revision Summary
  3. Class = blueprint, Object = instance with actual memory.
  4. Encapsulation: hide data via private/protected + expose via public methods.