Introduction to Object-Oriented Programming
Object-Oriented Programming (OOP) is a programming paradigm that organizes software around objects and classes. An object combines data and the operations that work on that data. OOP helps developers create modular, reusable, maintainable, and scalable software.
What is OOP?
OOP is a programming approach in which real-world entities or concepts are modeled as objects. An object contains data (state) and methods (behavior).
Key Concepts in OOP
- Class: A blueprint or template used to create objects.
- Object: An instance of a class.
- Method: A function or behavior defined inside a class.
- Attribute / Field: Data associated with an object or class.
- Inheritance: A mechanism through which a class can acquire properties and behavior from another class.
- Polymorphism: The ability of the same interface, method, or operation to behave differently depending on the object or context.
- Encapsulation: Bundling data and the methods that operate on it and controlling access to the data.
- Abstraction: Hiding unnecessary implementation details and exposing essential features.
Four Pillars of Object-Oriented Programming
The four commonly recognized fundamental principles of OOP are Encapsulation, Inheritance, Polymorphism, and Abstraction.
1. Encapsulation
Encapsulation combines data and the methods that operate on that data into a single unit and controls how the data is accessed.
2. Inheritance
Inheritance allows a new class to acquire or reuse properties and behavior of an existing class.
3. Polymorphism
Polymorphism allows the same operation or interface to produce different behavior depending on the object or context.
4. Abstraction
Abstraction hides unnecessary implementation details and exposes only the essential functionality.
Class and Object
Classes and objects are fundamental concepts of object-oriented programming.
Class
A class is a blueprint or template that defines the data and behavior that objects of that type can have.
Object
An object is an instance of a class. An object has state, behavior, and identity.
| Class | Object |
|---|---|
| Blueprint or template | Instance of a class |
| Defines data and behavior | Has actual state and behavior |
| Can be used to create objects | Created from a class |
| One class can create many objects | Each object has its own identity |
Encapsulation
Encapsulation is the practice of bundling data and the methods that operate on that data within a class and controlling access to the internal state.
How to Achieve Encapsulation
- Declare fields with restricted access, commonly private.
- Provide public methods when controlled access is required.
- Validate data before changing the object's state.
Benefits of Encapsulation
- Data Protection: Prevents direct uncontrolled access to internal data.
- Validation: Allows validation before modifying data.
- Maintainability: Internal implementation can change without changing how the class is used.
- Controlled Access: Provides read-only, write-only, or controlled access where appropriate.
Inheritance
Inheritance is a mechanism through which one class derives from another class and can reuse or extend its accessible members.
Important Terms
- Superclass / Base Class: The class being inherited from.
- Subclass / Derived Class: The class that inherits from the superclass.
Types of Inheritance
- Single Inheritance: One subclass inherits from one superclass.
- Multilevel Inheritance: A class inherits from a class that itself inherits from another class.
- Hierarchical Inheritance: Multiple subclasses inherit from the same superclass.
- Multiple Inheritance: A class inherits from multiple parent classes. Java does not support this through classes.
- Hybrid Inheritance: A combination of different inheritance types.
Benefits of Inheritance
- Code Reusability: Existing functionality can be reused.
- Extensibility: A subclass can add new functionality.
- Method Overriding: Supports runtime polymorphism.
- Hierarchical Classification: Related classes can be organized into a hierarchy.
Polymorphism
Polymorphism means "many forms". It allows the same method name, interface, or operation to represent different behavior depending on the context.
Types of Polymorphism in Java
1. Compile-Time Polymorphism
In Java, compile-time polymorphism is commonly achieved through method overloading.
Method overloading occurs when methods have the same name but different parameter lists.
2. Runtime Polymorphism
Runtime polymorphism is commonly achieved through method overriding, where a subclass provides its own implementation of an inherited method.
Overloading → Compile-time polymorphism
Overriding → Runtime polymorphism
Abstraction
Abstraction means hiding unnecessary implementation details and exposing only the essential functionality required by the user.
Ways to Achieve Abstraction in Java
- Abstract Classes: Classes declared using the abstract keyword.
- Interfaces: Define a contract that implementing classes must follow.
Abstract Classes
Interfaces
| Abstract Class | Interface |
|---|---|
| Can contain abstract and concrete methods | Can contain abstract methods and, in modern Java, default, static and private methods |
| Can have instance variables | Fields are implicitly public, static and final |
| Can have constructors | Cannot have constructors |
| A class can extend only one class | A class can implement multiple interfaces |
Constructors
A constructor is a special member of a class that is invoked when an object is created. It is commonly used to initialize an object's state.
Types of Constructors
- No-Argument Constructor: A constructor that accepts no arguments.
- Parameterized Constructor: A constructor that accepts one or more parameters.
- Copy Constructor: A programmer-defined constructor that initializes an object using another object of the same class.
Constructor Characteristics
- Has the same name as the class.
- Has no return type, not even void.
- Is invoked when an object is created.
- Can be overloaded.
- If a class declares no constructor, Java provides a compiler-generated default constructor.
Access Modifiers in Java
Access modifiers control the visibility and accessibility of classes, fields, methods, and constructors.
Types of Access Levels
| Modifier | Same Class | Same Package | Subclass | Other Package |
|---|---|---|---|---|
| private | Yes | No | No | No |
| default (package-private) | Yes | Yes | Only if in same package | No |
| protected | Yes | Yes | Yes* | Yes*, through inheritance |
| public | Yes | Yes | Yes | Yes |
Static Keyword
In Java, the static keyword indicates that a member belongs to the class rather than to individual objects. Static members are associated with the class itself.
Static Variables
- Also called class variables.
- Shared by objects of the same class.
- Only one class-level variable is maintained for the class.
Static Methods
- Belong to the class rather than individual objects.
- Can be called using the class name.
- Cannot directly access instance variables or instance methods.
- Cannot directly use this or super.
OOP vs Procedural Programming
Procedural programming organizes programs primarily around procedures or functions, whereas OOP organizes programs around objects and classes.
| Aspect | Procedural Programming | Object-Oriented Programming |
|---|---|---|
| Primary Organization | Functions / procedures | Classes and objects |
| Approach | Often follows a top-down design approach | Often associated with bottom-up design |
| Data and Behavior | Often handled separately | Often bundled within objects |
| Data Hiding | Generally less central to the paradigm | Encapsulation provides controlled access |
| Reusability | Can use functions and other techniques | Uses classes, composition, inheritance, etc. |
| Polymorphism | Not a defining feature | Important OOP feature |
| Examples | C, Pascal | Java, C++, C# |
Benefits of Object-Oriented Programming
OOP provides several mechanisms that can make large software systems easier to design, develop, test, and maintain.
Main Benefits of OOP
- Modularity: Programs can be organized into classes and objects.
- Reusability: Existing classes and components can be reused.
- Maintainability: Well-designed classes can make changes easier to manage.
- Scalability: Applications can be extended using new classes and components.
- Data Protection: Encapsulation allows controlled access to internal state.
- Flexibility: Polymorphism allows different implementations to be used through a common interface.
- Real-world Modeling: Real-world entities can be represented using objects.
- Team Development: Different developers can work on separate classes or components.
Real-world Applications of OOP
- Banking Systems: Account, Customer, Transaction
- E-commerce: Product, ShoppingCart, Customer, Order
- Game Development: Player, Enemy, Weapon
- GUI Applications: Button, Window, Menu
- Enterprise Applications: Customer, Employee, Invoice, Order
Quick Revision: OOP
| Concept | Meaning | Key Point |
|---|---|---|
| Class | Blueprint for objects | Defines data and behavior |
| Object | Instance of a class | Has state, behavior and identity |
| Encapsulation | Bundling and controlled access | Protects internal state |
| Inheritance | Acquiring/reusing class functionality | Promotes reuse and specialization |
| Polymorphism | Many forms | Overloading and overriding in Java |
| Abstraction | Hiding implementation details | Abstract classes and interfaces |
| Constructor | Initializes objects | No return type |
| Static | Belongs to the class | Shared class-level member |