OOP

Complete

Summary

Classes

ModifierPermissions
publicaccessible to any other class
defaultaccessible within the same package

Attributes, methods and constructors

ModifierPermissions
publicaccessible to any other class
privateaccessible only within the class
defaultaccessible within the same package
protectedaccessible only within the same package and subclasses

Relationships

Is-aHas-aCan-do
- inheritance
- extends and implements
- composition
- instance attributes
- ownership
- class abstraction
- interfaces and abstract methods

Associations

ConceptCore IdeaKey CharacteristicsExample
navigabilitywho can access whom in a relationshipunidirectional or bidirectional; based on object referencesPerson -> Cat (owner knows pet)
multiplicitynumber of objects involved in an association0..1, 1, 1..*; uses variables or collectionsone Student -> many Courses
dependencytemporary usage of another classno long-term link; no stored referencemethod parameter usage
compositionstrong whole–part relationshipparts depend on whole; no independent existenceEmail -> Subject
aggregationweak “has-a” relationshipparts can exist independentlyTeam -> Person
association classrelationship with its own datarepresented as a separate classMarriage (with date)

Concept

  • programming paradigm
  • way to reason about code

Objects

  • emulate real objects
  • have state and behaviour - encapsulation of data and related behaviour
  • the world is a network of interacting objects - send messages between objects
  • objects are an abstraction mechanism

Classes

  • contains instructions for creating objects
  • generalization for a ‘class’ of objects
  • class-level/static attributes and methods

Associations

  • connections between objects
  • can be dynamic
  • can be generalized as associations between classes
  • implemented as instance level variables

Navigability

  • if we can navigate from one object to another
  • one object knows about another
java
// bidirectional, owner <-> pet
class Person {
    Cat pet;
    //...
}

class Cat {
    Person owner;
    //...
}

// two unidirectional, owner -> pet, but pet -> breeder
class Person {
    Cat pet;
    //...
}

class Cat{
    Person breeder;
    //...
}

two unidirectional != one bidirectional

Multiplicity

  • how many objects take part in each association
  • optional or compulsory

Dependency

  • object depends on another object without a direct association
  • associations are implicit dependencies, this is just to highlight less obvious ones
java
class Foo {
    int calculate(Bar bar) { // Foo depends on Bar, but doesn't have a direct association
        return bar.getValue();
    }
}

class Bar {
    int value;

    int getValue() {
        return value;
    }
}

Composition

  • strong whole-part relationship
  • the parts cannot exist without the whole
  • no cyclical links
  • depends on context
  • cascading deletion != composition

Aggregation

  • container-contained relationship
  • weaker than composition
  • part can exist even without the whole
java
class Team {
    Person leader;
    ...
    void setLeader(Person p) {
        leader = p;
    }
}

Association classes

  • additional information about an association
java
class Transaction {
    // all fields are compulsory
    Person seller;
    Person buyer;
    Date date;
    String receiptNumber;

    Transaction(Person seller, Person buyer, Date date, String receiptNumber) {
        // set fields
    }
}

Inheritance

  • is-a relationship

Overriding

  • subclass changes the behaviour of the parent class
  • dynamic binding - choose which implementation at runtime

Overloading

  • multiple methods with the same name but accept different type signatures
  • static binding - choose method at compile time

Interfaces

  • behaviour specification

Abstract class

  • class is just a representation of common data and behaviour among subclasses

Substitutability

  • subclass instances are super class instances

Polymorphism

  • write code targetting super class
  • when used on subclass objects, dynamic binding gives different results per subtype