title | description | _class | footer | _footer | paginate | _paginate | marp | theme | |
---|---|---|---|---|---|---|---|---|---|
OOP Basics |
Content for Eteration Bootcamp, 2022 |
|
OOP Basics | Deniz Memis |
true |
false |
true |
d8niz |
- bachelor of eng in eee.
- master's degree in etm.
- softare/r&d engineer.
denizmemis
@linkedin.comd8niz
@github.comdeniz.memis
@eteration.com
- objects
- classes
- inheritance
- abstraction
- hierarchies / taxonomies
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.
-
Expectation:
- Inheritance
- Taxonomy
-
Reality:
->
Cells-->
Tissues---->
Organs-------->
Organisms
"...a software development paradigm where functions are implicitly coded in every step required to solve a problem..."
"...a computer programming model that organizes software design around data, or objects, rather than functions and logic..."
-
Expectation:
- Objects (!)
- Classes (!)
-
Reality:
- Messaging
- Late-binding
- Say objects are like cells
- And big idea is messaging
- How do they even communicate ?
- Dopamine (look slightly right)
Receptors
acting as communication agents- Laying out the messaging protocol
- What do you see here?
-
Computers messaging thru an established protocol
-
What do we get ?
- Much larger and flexible system
- Handles complexity
- Handles scaling
<style scoped> pre { font-size: 1rem; background-color: #000 } </style>
thing.do(some, stuff);
<rec> <---message--->
<---args--->
- Email format:
- to: thing
- subject: urgent please :)
- message: 'do', some, stuff
<style scoped> pre { font-size: 1rem; background-color: #000 } </style>
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) todo
what to do :) - Flexible
- Adaptable
- Tolerant to change
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
"...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
}
}
"...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) {...}
}
- 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
<style scoped> pre { font-size: 0.5rem; background-color: #000 } </style>
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());
}
}
- 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