Object-Oriented Design Notes
Object-Oriented Design: OOP Fundamentals
Core Concepts of OOP
- A-Pie Framework: The four core concepts are:
- Abstraction: Hiding complex realities while exposing only the necessary parts.
- Polymorphism: The ability to present the same interface for differing underlying data types.
- Inheritance: The mechanism by which one class can inherit properties and behaviors from another.
- Encapsulation: Bundling the data with the methods that operate on that data.
Understanding Encapsulation
- Etymology: Derived from Merriam-Webster, meaning to cover completely to isolate.
- Definition: Encapsulation involves bundling attributes (data) and behaviors (methods) within a class while controlling their visibility.
- Visibility Control: Determines access levels for the attributes:
- Read-only: Can only be accessed via getters.
- Write-only: Can only be set via setters.
- Read/Write: Both getters and setters provided.
- Invisible: No access to the variable.
Benefits of Encapsulation
- Organized Code: Related functionalities are grouped within a single class, improving structure.
- Access Control: Prevents unintended interference with class data and behaviors, acting as a protective barrier.
- Differentiating Terms: While some refer to this as "data hiding" or "information hiding", encapsulation is more about managing access and bundling attributes with behaviors.
Mutable vs Immutable Objects
- Mutable Class: A class is termed mutable if its state can change; hence, setters that change attributes are known as "mutators".
Introduction to Interfaces
- Interface Definition: The interface represents the set of visible members of a class that can be interacted with externally.
- Separating Interfaces: Interfaces can be defined in separate files.
Creating an Interface Example
public class Dog {
private String voice;
public void printVoice() {
System.out.println(voice);
}
}
public interface DogInt {
public void printVoice();
}
- Naming Convention: Add prefixes or suffixes like "Int" or "I" to distinguish interfaces from classes.
Practical Usage of Interfaces
- Implementation Phase: Designers typically define interfaces before implementation; classes will later implement these interfaces.
- Example Code:
public interface AnimalInt {
public void printVoice();
}
public class Dog implements AnimalInt {
private String voice;
public void printVoice() {
// code...
}
}
- "Is-a" Relationship: When a class implements an interface, it is said to possess an "is-a" relationship with the interface.
Understanding UML in Interfaces
- UML Connectors: Specific connectors are used to represent relationships among classes, including interfaces.
- Documenting Relationships:
- Dependency, Aggregation, Composition, Inheritance, Interface Implementation, Association are types of relationships to note.
- In UML, relationships can be represented visually to identify how classes interact with one another.
Interface Components
- Abstract Methods: Methods that are declared without implementations in an interface and are public and abstract by nature.
- Default Methods: Introduced in Java 8, can be implemented within an interface using the "default" keyword.
- Constants in Interfaces: Should be public final static. Defaults adhere to this unless keywords are specifically omitted.
- Static Methods: Can also exist in interfaces since Java 8.
- No Instance Variables: Interfaces cannot declare instance variables.
Key Notes on Interfaces
- Specification Role: Interfaces stipulate what should be done without dictating how it should be done.
- Multiple Implementations: A class can implement multiple interfaces, enhancing flexibility in design.
- Extending Interfaces: An interface can extend other interfaces, sharing similar terminology as class inheritance, but classes themselves can only inherit from one class.