Who Am I?

  • bachelor of eng in eee.
  • master's degree in etm.
  • softare/r&d engineer.
  • denizmemis @linkedin.com
  • d8niz @github.com
  • deniz.memis @eteration.com
OOP Basics | Deniz Memis

Object Oriented Programming

  • objects
  • classes
  • inheritance
  • abstraction
  • hierarchies / taxonomies
OOP Basics | Deniz Memis

Story Time :)

OOP Basics | Deniz Memis

Alan Kay

Personal computing pioneer (see Smalltalk), holding a Dynabook 💻

  • The best way to predict the future is to invent it.

  • Technology is anything invented after you were born.

  • The most disastrous thing that you can ever learn is your first programming language.

  • A change in perspective is worth 80 IQ points.

OOP Basics | Deniz Memis

OOPs #1: Human Cell Analogy

  • Expectation:

    • Inheritance
    • Taxonomy
  • Reality:

    • ->Cells
      --> Tissues
      ----> Organs
      --------> Organisms
OOP Basics | Deniz Memis

Contrast: Imperative (Procedural) Style

"...a software development paradigm where functions are implicitly coded in every step required to solve a problem..."

OOP Basics | Deniz Memis

Contrast: Object-Oriented Style

"...a computer programming model that organizes software design around data, or objects, rather than functions and logic..."

OOP Basics | Deniz Memis

OOPs #2: Big Idea

  • Expectation:

    • Objects (!)
    • Classes (!)
  • Reality:

    • Messaging
    • Late-binding
OOP Basics | Deniz Memis

Objects

  • Say objects are like cells
  • And big idea is messaging
  • How do they even communicate ?
OOP Basics | Deniz Memis

Messages

  • Dopamine (look slightly right)
  • Receptors acting as communication agents
  • Laying out the messaging protocol
OOP Basics | Deniz Memis

Systems

  • What do you see here?
  • Computers messaging thru an established protocol

  • What do we get ?

    • Much larger and flexible system
    • Handles complexity
    • Handles scaling
OOP Basics | Deniz Memis

Typical OOP Syntax

thing.do(some, stuff);
<rec> <---message--->
         <---args--->
  • Email format:
- to: thing
- subject: urgent please :)
- message: 'do', some, stuff
OOP Basics | Deniz Memis

What does do do ?

thing.do(some, stuff);
thing.do(other, stuff)
child.do(some, stuff)
thing.do(some, stuff)
  • Late binding
  • It is up to the thing (object || recipient) to do what to do :)
  • Flexible
  • Adaptable
  • Tolerant to change
OOP Basics | Deniz Memis

End Of Story Time :)

OOP Basics | Deniz Memis

Four Pillars Of OOP

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
OOP Basics | Deniz Memis

Abstraction

"...the process of considering something independently of its associations or attributes..."

  • Hiding implementation details
abstract class Bike{  
  abstract void run();  
}  

class HondaCB250R extends Bike{  
  void run()
  {
    System.out.println("running safely on a 250cc bike");
  }

  public static void main(String args[])
  {  
    Bike obj = new Honda4();  
    obj.run(); //running safely on a 250cc bike
  }  
}  
OOP Basics | Deniz Memis

Encapsulation

"...the action of enclosing something in or as if in a capsule..."

  • Bundling the data along with the behaviior
  • Facilitates the state management, keeps thing organized, allows read/write access management
class Person {
  private int age;
  private String name;
  private String surname;
  private float salary;
  private String pob;
  private String dob;

  public Person(name,surname){...}

  public int getAge(){...}
  public void setAge(int age) {...}
  .
  .  
  public float calculateSalary(int age, String dob) {...}

}
OOP Basics | Deniz Memis

Inheritance

  • Some classes may share commonalities
    • For example HomePolicy, AutoPolicy, LifePolicy classes may all have same state and behavior
  • Instead of repeating commonalities in each class, we can abstract them in a common place
  • These commonalities can be stored in a super class
  • Each subclass inherits state and behavior from its superclass
OOP Basics | Deniz Memis

Inheritance (cont'd)

public class Main {
	public static void main(String[] args) {
		Calc calc = new Calc();
		System.out.println("sum: " + calc.add());

		Calc calcWihParametricConstuctor = new Calc(1, -2);
		System.out.println("sum: " + calcWihParametricConstuctor.add());

		AbsoluteCalc absCal = new AbsoluteCalc(1, -2);
		System.out.println("abs sum: " + absCal.add());
	}
}

class Calc {
	private int first;
	private int second;

	public Calc(int first, int second) {
		this.first = first;
		this.second = second;
	}

	public int add() {
		return this.first + this.second;
	}
}

class AbsoluteCalc extends Calc {

	public AbsoluteCalc(int first, int second) {
		super(first, second);
	}

	@Override
	public int add() {
		return Math.abs(this.getFirst()) + Math.abs(this.getSecond());
	}
}
OOP Basics | Deniz Memis

Polymorphism

  • Different objects respond to the same message in different ways
    • For example when asked to talk a dog barks, and a cat meows
  • It is often supported by method overriding
    • Overriding means that subclass may implement the same method as superclass, but with different code
    • toString() method in the Object class is an example of often overridden method
OOP Basics | Deniz Memis
OOP Basics | Deniz Memis

* Single unit * Absolute coherence & harmony * _Weak Link_ Theory