Object Oriented Programming: The Four Pillars of OOP

The Four Pillars:

Encapsulation

Achieving Encapsulation

public class Person {
  private String name;
  private Integer age;
  public Person(String name, Integer age) {
    this.name = name;
    this.age = age;
  }
  // getters and setters omitted for brevity
}

Encapsulation
Managing person-data
without Encapsulation

// setting name and age of leon
String leonName = "Leon";
Integer leonAge = 25;

// setting name and age of wilhem
String wilhemName = "Wilhem";
Integer wilhemAge = 23;

// changing state of leon
leonName = "Lee";
leonAge = 26;

// changing state of wilhem
wilhemName = "Wil";
wilhemAge = 24;

Encapsulation
Managing person-date
with Encapsulation

public void demo() {
    // setting name and age of leon
    Person leon = new Person("Leon", 25);

    // setting name and age of wilhem
    Person wilhem = new Person("Wilhem", 23);

    // changing name of leon
    leon.setName("Lee");
    leon.setAge(26);

    // changing name of wilhem
    wilhem.setName("Wil");
    wilhem.setAge(24);
}

Inheritance

How to achieve Inheritance

Inheritance
Designing an Animal class

public class Animal {
  private Point position;
  protected String phrase;

  public Animal(String phrase) { this.phrase = phrase; }

  public void setPosition(Integer xPosition, Integer yPosition) {
    this.position = new Point(xPosition, yPosition);
  }

  public Point getPosition() { return this.position; }
}

Inheritance
Designing a Fox class

public class Fox extends Animal {
  public Fox() {
    super("Bark!");
  }

  public void useSpeech() {
    System.out.println(super.phrase); // inherited member
  }
}

Inheritance
Accessing Animal Properties

public void demo() {
  Fox fox = new Fox();
  Point foxPosition = fox.getPosition() // method of `Animal` class
  System.out.println(foxPosition);
}

Output

null

Inheritance
Accessing Animal Properties

Polymorphism

Dynamic Polymorphism

Dynamic (Runtime) Polymorphism
Designing Bird class

public interface Flyer { void fly(int distance); }
public abstract class Animal { String speak(); }
public class Bird extends Animal implements Flyer {
  @Override
  public void fly(int distance) {
    this.setLocation(getX(), getY()+distance);
  }

  @Override
  public String speak() {
    return "chirp!"
  }
}

Dynamic (Runtime) Polymorphism
Bird Example

public void demo() {
  Animal animal = new Bird();
  animal.speak();
}

Dynamic (Runtime) Polymorphism
Bird Example

public void demo() {
  Flyer flyer = new Bird();
  flyer.fly(10);
}

Dynamic (Runtime) Polymorphism
Bird Example

public void demo() {
  Bird bird = new Bird();
  bird.speak();
  bird.fly(10);
}

Static Polymorphism

Static (compile-time) Polymorphism
Designing Person class

public class Person {
  private String name;
  private Integer age;
  private Color eyeColor;

  public Person(String name, Integer age, Color eyeColor) {
    this.name = name;
    this.age = age;
    this.eyeColor = eyeColor;
  }

  public Person() {
    this("John", 50, Color.BLACK);
  }
}

Static (compile-time) Polymorphism
Person Example

public void demo() {
  String personName = "Douglas";
  Integer personAge = 25;
  Color personEyeColor = Color.BLUE;
  Person person = new Person(personName, personAge, personEyeColor);

  System.out.println(person.getName());
  System.out.println(person.getAge());
  System.out.println(person.getEyeColor());
}

Output

Douglas
25
BLUE

Static (compile-time) Polymorphism
Person Example

public void demo() {
  Person person = new Person();

  System.out.println(person.getName());
  System.out.println(person.getAge());
  System.out.println(person.getEyeColor());
}

Output

John
50
BLACK

Abstraction

Abstraction

Abstraction

public void withAbstraction() {
    IOConsole console = new IOConsole();
    String promptName = "What is your name?";
    String promptAge = "What is your age?";
    String userInputName = console.getStringInput(promptName);
    Integer userInputAge = console.getIntegerInput(promptAge);
    Person person = new Person(userInputName, userInputAge);
    console.print(person.toString());
}