Goodbye, Object Oriented Programming

Inheritance, the First Pillar to Fall

Banana Monkey Jungle Problem

Banana Monkey Jungle Solution

The Diamond Problem

Class PoweredDevice {
}
Class Scanner inherits from PoweredDevice {
function start() {
}
}
Class Printer inherits from PoweredDevice {
function start() {
}
}
Class Copier inherits from Scanner, Printer {
}

The Diamond Solution

Class PoweredDevice {
}
Class Scanner inherits from PoweredDevice {
function start() {
}
}
Class Printer inherits from PoweredDevice {
function start() {
}
}
Class Copier {
Scanner scanner
Printer printer
function start() {
printer.start()
}
}

The Fragile Base Class Problem

import java.util.ArrayList;

public class Array
{
private ArrayList<Object> a = new ArrayList<Object>();

public void add(Object element)
{
a.add(element);
}

public void addAll(Object elements[])
{
for (int i = 0; i < elements.length; ++i)
a.add(elements[i]); // this line is going to be changed
}
}
public class ArrayCount extends Array
{
private int count = 0;

@Override
public void add(Object element)
{
super.add(element);
++count;
}

@Override
public void addAll(Object elements[])
{
super.addAll(elements);
count += elements.length;
}
}
  public void addAll(Object elements[])
{
for (int i = 0; i < elements.length; ++i)
add(elements[i]); // this line was changed
}

The Fragile Base Class Solution

The Hierarchy Problem

The Hierarchy Solution

Document
Company
Handbook

Encapsulation, the Second Pillar to Fall

The Reference Problem

The Reference Solution

Polymorphism, the Third Pillar to Fall

Broken Promises

So then what?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Charles Scalfani

Charles Scalfani

Software Engineer and Architect, Teacher, Writer, Filmmaker, Photographer, Artist…