notes

this page could be a lot more – largely because of the huge power of Object-Oriented-Programming.

instead I have chosen to limit the scope of this page to simple definitions.

this page pairs well with:

Definition (Data)
Ultimately, programming is about taking some data and then manipulating it. Either in-place or manipulating other data in another place. This is why Object-Oriented-Programming is so powerful: because it encapsulates the data with the methods which manipulate it:
Example (Circle Class)

public class Circle extends Object {
  // data:
  private static final double PI = 3.14159;
  public int x = 0, y = 0;
  private int r = 6;
  static int no_circles = 0;

  // constructors:
  public Circle() { super(); no_circles++; }
  public Circle(int x, int y, int r) { this(); this.x=x; this.y=y; this.r=r; }
  public Circle(int x, int y) { this(x,y,5); }

  // methods:
  public double getArea() { return PI * r * r; }

  @Override public String toString() {
    return "[" + x + ", " + y + " r=" + r + "]";
  }

  @Override public boolean equals(Object obj) {
    if (obj == this) return true;
    if (!(obj instanceof Circle other)) return false;
    return x==other.x && y==other.y && r==other.r;
  }

  // Getters/Setters
  public int getR(){ return r; }
  public void setR(int r){ if(r>0) this.r=r; }
}

Definition (Object)
An object is an instance of a class. Something we can create with
Animal lion = new Animal()
, where lion is the variable name, Animal is the type, and Animal() calls the constructor of the Animal class to return an object of that type.

Definition (Class)

A class is a blueprint for the state (data/fields) and behaviours (methods) of an object. It is reused for the creation of objects and can make use of some clever patterns to improve scalability.

One thing to remember with OOP is its adjacency with real-world modelling: I, Aayush Bajaj am an object; produced from the class People. I have attributes, that can either be private or public; i.e. I can tell information to members outside of my class / package, or I could not.

Definition (Sub-classes)
A sub-class extends a superclass; inheriting accessible fields/methods and possibly overriding behaviour. This pattern should be used carefully because it results in a tight coupling of code. It is only appropriate for "is-a" relationships.
Remark
be careful to preserve the Liskov Substitution Principle

Definition (Inheritance)

Inheritance allows subtype polymorphism, but this is not the only way to achieve polymorphism.

Inheritance tightly couples the code. The "is-a" relationship is a necessary, but not sufficient condition for using inheritance.

Many times composition is still preferred to inheritance despite the "is-a".

Definition (Composition)

Embodies the "has-a" relationship. Builds objects from parts, favouring encapsulation and changeability.

class Engine { void start(){} }
class Car { private final Engine e = new Engine(); void start(){ e.start(); } }

Definition (Coupling)

Degree of interdependence (between what?). Symptoms of high coupling include:

Remark
Aim for low coupling and high cohesion

Definition (Cohesion)
Is measured by how well-related the functionalities are of a certain class. Unrelated methods imply low cohesion.

Definition (Refactoring)
  • Noun: a change made to the internal structure of software to make it easier to understand and cheaper to modify without change its observable behaviour
  • Verb: to restructure software by applying a series of refactorings without changing its observable behaviour.

Definition (Interfaces)

Pure contracts: types without implementation or state.

interface PaymentGateway { boolean charge(long cents); }
class StripeGateway implements PaymentGateway { public boolean charge(long c){ return true; } }

Definition (Abstract Classes)

Partial implementations with state and constructors; can define template methods and share code among subclasses.

abstract class Shape {
  abstract double area();
  final double scaleTo(double k){ return k*k*area(); } // template utility
}
class Rect extends Shape { double w,h; Rect(double w,double h){this.w=w;this.h=h;} double area(){return w*h;} }

Definition (Polymorphism)

Translates to mean "many forms". It allows objects of different types to be treated as objects of a common type.

There are two main types:

  1. Compile-time Polymorphism:

    • method overloading. a class may have many methods with the same name, but chooses the correct one based on the parameters at compile-time.
  2. Run-time Polymorphism:

    • method overriding. allows the implementation in a subclass to be used instead of the implementation in the superclass. this choice is made at run-time.
    • subtype polymorphism / LSP. is the replacement of a super-class with an object of its subclass.

abstract class vs. interface

this was a point of confusion for me during an interview:

feature abstract class interface
method implementation can have abstract methods and concrete methods always abstract only
state (variables) all types only public, static, final
constructors allowed not allowed
inheritance can only extend one abstract class can implement multiple interfaces
relationship "is-a" "can-do" or "has-a capability"