AA

OOP D.1 Objects as programming concept

Object-Oriented Programming Overview

  • Object Oriented Programming (OOP) focuses on using objects to design and implement software systems.

  • Key concepts include objects, classes, instantiation, and relationships between objects.

D.1 Objects as a Programming Concept

D.1.1 General Nature of an Object

  • Definition: An object is an abstract entity that has:

    • Data/Attributes/State: Characteristics that define the object.

    • Actions/Methods/Behaviors: Functions that the object can perform.

  • Common examples include:

    • People

    • Cars

    • Fractions

    • Dates

    • Music tracks

Attributes and Behavior

  • All objects have:

    • State: Represents attributes (in Java: fields).

    • Behaviour: Represents actions (in Java: methods).

  • Task: Identify a real-world object and its attributes and behaviours.

D.1.2 Object vs. Instantiation

  • Class: A block of code that defines the properties and behaviour of objects.

    • Can create multiple instances (objects) from a single class.

    • Each object has reserved memory for its data but shares methods defined in the class.

  • Instantiation: Creating an object from a class.

    • The new keyword in Java is used for instantiation, allocating memory and invoking the constructor.

    • Example: new Car("Toyota", "Camry") creates a new Car object.

Object Creation in Java

  • Object:

    • A fundamental concept in OOP is instances of classes representing real-world entities.

    • Hold attributes and behaviours.

  • Instantiation:

    • The act of creating an instance of a class.

    • Involves memory allocation and constructor call.

  • Example: Creating a Dog object demonstrates instantiation and method invocation using dot notation.

Object References

  • Object references in memory allow the linking of variables to objects:

    • Example declaration: Dog d; d = new Dog(); where d refers to a Dog object.

D.1.3 & D.1.4 UML Diagrams

  • UML (Unified Modeling Language): Visual tool to depict class definitions and relationships.

  • Class Diagram Structure:

    • Class Name

    • Attributes

    • Methods

  • Access Modifiers:

    • + For public and - for private.

D.1.5 Decomposition into Related Objects

  • Class Decomposition: Breaking a programming problem into multiple classes, each fulfilling a specific task.

  • Example: A computer program for a school might include classes for principals, teachers, and counsellors.

D.1.6 Object Relationships

  • Types of Relationships:

    • Dependency: One class depends on another (e.g., a borrower depends on a book).

    • Inheritance: One class inherits properties/methods from another, denoted with is-a relationships in UML.

    • Association: Objects interact but exist independently (denoted with has-a notation).

    • Aggregation: A special case where one object requires another but the latter can exist independently.

  • Multiplicity: Defines possible numbers of instances participating in relationships (e.g., 1, 0..1, *, etc.).

D.1.7 Reducing Dependencies

  • High coupling can create issues; aim for low coupling (decoupling).

  • Techniques to achieve this include encapsulation and interfaces.

D.1.8 Object Construction in Exams

  • Students may need to create UML diagrams or code from descriptions.

  • Important considerations include inheritance and containment relationships.

D.1.9 Representing Data Items with Types

  • Different data types allow for safe operations on variables, impacting performance and efficiency.

D.1.10 Passing Data Items as Parameters

  • Parameters: Variables defined for use within methods/functions.

  • Pass-by-Value: Sends a copy of the variable's value (used for primitive types).

  • Pass-by-Reference: Sends the memory location, allowing methods to modify the original object.

    • Important distinctions involve mutable vs immutable types (e.g., Strings are immutable).