Definition
Concept
Object: instance of a class encapsulating data and behavior. Core abstraction in object-oriented programming (OOP). Represents entities with state and operations.
Distinction from Class
Class: blueprint or template. Object: concrete realization of class. Multiple objects may share class but differ in state.
Role in OOP
Enables modularity, reusability, abstraction. Facilitates modeling of real-world entities and systems.
Structure of Objects
Attributes (Properties)
Data fields holding object state. Typed variables defined by class. Example: int age; String name.
Methods (Behaviors)
Functions defining actions object can perform. Operate on object's data. Example: void walk(), int getAge().
Identity
Unique reference distinguishing object from others. Two objects can have identical state but different identity.
Encapsulation Boundary
Defines access to internal state. Protects integrity by restricting direct external manipulation.
Creation and Instantiation
Instantiation Process
Allocates memory, initializes state using constructor. Produces runnable object from class template.
Constructors
Special methods to initialize new objects. Can be overloaded for multiple initialization schemes.
Factory Methods
Alternative creation pattern. Encapsulates instantiation logic. Useful for abstraction and control.
Example: Java Instantiation
MyClass obj = new MyClass(); creates object using constructor.
Encapsulation
Definition
Mechanism to bundle data and methods. Restricts direct access to some components.
Access Modifiers
Keywords controlling visibility: public, private, protected. Enforce encapsulation boundaries.
Benefits
Prevents unauthorized access. Simplifies interface. Supports modularity and maintainability.
Example
Private data fields accessed via public getter/setter methods.
Inheritance
Definition
Mechanism allowing new class to inherit attributes and methods from existing class.
Types
Single, multiple (language-dependent), multilevel inheritance. Enables hierarchical classification.
Benefits
Code reuse, polymorphic behavior, logical organization of related classes.
Method Overriding
Subclass redefines superclass method to alter or extend behavior.
Polymorphism
Definition
Ability of objects of different classes related by inheritance to respond differently to same message (method call).
Static vs Dynamic
Static: compile-time binding (method overloading). Dynamic: runtime binding (method overriding).
Use Cases
Interfaces, abstract classes enable polymorphic behavior. Facilitates flexible and extensible code.
Example
Calling draw() on shape object invokes subclass-specific implementation.
Methods and Behavior
Definition
Code blocks defining object operations. Implement class behavior.
Instance vs Static Methods
Instance: operate on object state. Static: belong to class, no object state access.
Method Signatures
Includes method name, parameters, return type. Determines method identity.
Example
public int getAge() { return age; } returns object's age.
Properties and State
Definition
Variables encapsulated in object representing current data values.
Mutability
Properties can be mutable or immutable. Controlled via accessors.
Data Types
Primitive, reference types. Define storage and behavior constraints.
Example Table of Common Properties
| Property | Type | Description |
|---|---|---|
| name | String | Identifier of object |
| age | int | Numeric state value |
| isActive | boolean | Status flag |
Memory Management
Allocation
Objects allocated on heap memory. Managed by runtime environment.
Garbage Collection
Automatic reclamation of unreachable objects. Prevents memory leaks.
Reference Counting
Alternative technique tracking references for deallocation.
Impact on Performance
Excessive object creation may degrade performance. Object pooling reduces overhead.
Object Lifecycle
Phases
Creation, usage, modification, destruction. Lifecycle managed by program flow and runtime.
Constructor and Destructor
Constructor initializes state. Destructor or finalizer handles cleanup.
State Transitions
Objects may change state via methods. State consistency ensured by encapsulation.
Example Lifecycle Diagram
+------------+ create +------------+| NULL | --------------> | ALLOCATED |+------------+ +------------+ | initialize V +------------+ | ACTIVE | +------------+ | destroy V +------------+ | COLLECTED | +------------+ Design Principles
Single Responsibility
Object should have one reason to change. Simplifies maintenance.
Open/Closed Principle
Objects open for extension, closed for modification.
Liskov Substitution Principle
Subtype objects must be substitutable for base types without altering correctness.
Interface Segregation
Clients should not be forced to depend on interfaces they do not use.
Practical Examples
Example: Class and Object in Java
public class Car { private String model; private int year; public Car(String model, int year) { this.model = model; this.year = year; } public void drive() { System.out.println(model + " is driving."); } public int getYear() { return year; }}Car myCar = new Car("Toyota", 2020);myCar.drive(); // Output: Toyota is driving. Example: Polymorphism in C++
class Shape {public: virtual void draw() = 0;};class Circle : public Shape {public: void draw() override { std::cout << "Draw Circle" << std::endl; }};class Square : public Shape {public: void draw() override { std::cout << "Draw Square" << std::endl; }};Shape* s1 = new Circle();Shape* s2 = new Square();s1->draw(); // Draw Circles2->draw(); // Draw Square Comparison Table: Object vs Class
| Aspect | Class | Object |
|---|---|---|
| Definition | Template/Blueprint | Instance of a class |
| State | No specific state | Has specific values |
| Behavior | Defines methods | Executes methods |
| Memory | No direct memory allocation | Allocated on heap or stack |
References
- Booch, G., Object-Oriented Analysis and Design, Addison-Wesley, 1994, pp. 1-34.
- Liskov, B., Wing, J.M., "A Behavioral Notion of Subtyping," ACM Transactions on Programming Languages and Systems, Vol. 16, 1994, pp. 181-211.
- Gamma, E., Helm, R., Johnson, R., Vlissides, J., Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley, 1994, pp. 23-45.
- Schildt, H., Java: The Complete Reference, McGraw-Hill, 2018, pp. 120-165.
- Stroustrup, B., The C++ Programming Language, Addison-Wesley, 2013, pp. 200-250.