Object-Oriented Programming Notes
Object-Oriented Programming (OOP) in IB Computer Science
What is OOP?
A programming paradigm that organizes code into classes and objects, allowing for a modular and organized approach to software development. It enables programmers to model real-world entities through classes (templates) and objects (instances of classes), promoting better code management, reusability, and scalability.
Key Concepts of OOP
Class and Object:
Class: A blueprint for creating objects that defines the attributes (data fields) and behaviors (methods/functions) associated with the object.
Object: An instance of a class that holds the class's data and methods. Objects interact with one another to perform tasks.
Attributes and Behaviors:
Attributes: Variables that hold data relevant to an object, essentially properties of the object.
Behaviors: Methods (functions) that outline the actions an object can perform. They are crucial in defining how an object will behave in various situations.
Instantiation:
The process of creating an object from a class, which involves allocating memory for the object and initializing its attributes with specific values.
Example:
```java
Employee emp = new Employee();
This line of code creates an instance called `emp` of the `Employee` class.
Constructors
- Special methods that are called when an object is created, responsible for initializing the instance variables of the object.
- A class can define multiple constructors that are differentiated by their parameter types and counts (overloading).
- **Example**:
java
public Employee(String name, int id) {
this.name = name;
this.employee_id = id;
}
This constructor initializes a new `Employee` object with specified `name` and `id`.
this Keyword
- The `this` keyword refers to the current object instance, useful for distinguishing instance variables from method parameters that have the same name.
- It helps clarify variable scope within constructors and methods.
- **Example**:
java
public Person(String name, int age) {
this.name = name;
this.age = age;
}
Here, `this.name` refers to the instance variable while `name` refers to the parameter passed to the constructor.
Inheritance
- A powerful mechanism that allows the creation of new classes from existing ones, establishing a parent-child relationship. This promotes code reuse and can make software easier to maintain.
- The **Superclass** (parent) can pass down attributes and behaviors to the **Subclass** (child), allowing subclasses to inherit properties and methods from the superclass.
- **Example of inheritance**:
java
public class Salesman extends Employee {
double commission_rate;
// Additional behavior and attributes
}
In this case, the `Salesman` class inherits from the `Employee` class, gaining its attributes and behaviors while also adding specific features.
Aggregation
- A design relationship where one object contains one or more other objects, signifying a "has-a" relationship.
- **Example**: A `Salesman` class may aggregate multiple `Sale` instances, meaning that a salesman can manage several sales, each represented as a separate object of the `Sale` class.
Polymorphism
- The ability of different classes to be treated as instances of the same class through a common interface or base class, enabling methods to be executed in different forms.
- **Method Overloading**: This occurs when multiple methods have the same name within a class but different parameters, allowing them to perform different functions based on their input.
- **Method Overriding**: When a child class redefines a method inherited from the parent class to provide specific functionality for the child class.
Encapsulation
- The concept of bundling the data (attributes) and methods that operate on the data into a single unit (class). This promotes data hiding, where the internal state of an object is protected from unintended interference and misuse.
- Access to the internal state is often controlled using access modifiers.
Access Modifiers
- Keywords used to set the accessibility (visibility) of class members (attributes/methods):
- **public**: The class member is accessible from any other class.
- **private**: The class member is accessible only within the class it is defined. This protects data integrity.
- **protected**: The class member is accessible within the class and by subclasses that inherit from it, promoting safe inheritance and extension of the functionality.
Static Methods
- Methods that belong to the class itself rather than to instances of the class. This means they can be called without an object, providing functionality related to the class as a whole.
- **Example**:
java
public static int calculateTax(int salary) {
return salary * 0.30;
}
```
This static method calculates tax based on a salary input without requiring an instance of the class.
Modularity
The principle of breaking down a program into smaller, manageable pieces or modules. Each module can handle specific tasks, contributing to the overall functionality of the software.
Advantages include easier debugging, simplified development, enhanced code readability, and improved code reusability across multiple projects.
UML Diagrams
Unified Modeling Language (UML) diagrams are used to model the structure of a system, visually representing classes, attributes, methods, and their relationships in an organized manner. These diagrams help in understanding the overall architecture of the system and facilitate effective communication among team members.
Pros and Cons of OOP
Pros:
Code reusability through inheritance leads to efficient development.
Modularity and maintainability make it easier to manage complex systems.
Encapsulation enhances security and minimizes the risk of errors due to uncontrolled access to data.
Cons:
Increased complexity can arise for smaller projects that may not need advanced OOP features, making it cumbersome.
Typically requires more memory usage compared to more straightforward procedural programming techniques.
Terminology
Object: A unit of data that combines both state (attributes) and behavior (methods).
Class: A blueprint that defines the structure and behavior of the objects created from it.
Accessor Method: A method that retrieves the value of an object's attribute without modifying it, commonly referred to as a getter.
Mutator Method: A method that modifies an object's attribute, commonly referred to as a setter.
Method Signature: The unique identification of a method, comprising the method's name along with its parameters.
Basic Programming Tasks
Implement classes with the necessary attributes and behaviors defined in terms of methods.
Use constructors effectively to initialize object state appropriately.
Implement relationships such as aggregation and inheritance to promote good design practices.
Utilize accessors and mutators efficiently to manage data and maintain encapsulation principles in the program.