OOP?
Lecturer Gant Laborde
To not only show Object Oriented Programming, but to provide the reasoning and feel the solution actually solve a real world problem for software as a paradigm.
JavaScript in this workshop is ES6
Top Down
Imagine the above as an english essay.
How do we solve this?
A strategy for breaking code into parts.
Objects are instances of classes
class Cup
def initialize
puts "I'm alive! *SPARKLE*"
@drink_amount = 0
end
def fill
puts "I'm filled up!"
@drink_amount = 100
end
def empty
puts "ALL GONE!"
@drink_amount = 0
end
def quantity
puts @drink_amount
@drink_amount
end
end
Constructor
Class
Methods
Constructor
Class
Methods
public class Cup {
public int drinkAmount = 0;
public Cup() {
System.out.println("I'm alive! *SPARKLE*");
drinkAmount = 0;
}
public void fill () {
System.out.println("Fill'er up!");
drinkAmount = 100;
}
public void empty () {
System.out.println("Dump it!");
drinkAmount = 0;
}
public int quantity () {
System.out.println(drinkAmount.toString() + " is left");
return drinkAmount;
}
}
Constructor
Class
Methods
class Cup {
constructor() {
console.log("I'm alive!")
this.drinkAmount = 0
}
fill () {
console.log('Cup fillled up')
this.drinkAmount = 100
}
empty () {
console.log('Cup emptied')
this.drinkAmount = 0
}
quantity () {
console.log(`${this.drinkAmount} drink left`)
return this.drinkAmount
}
}
JS Bin
https://jsbin.com/buwucuk/1/edit?js,console
Ruby
def self.volume radius, height
puts Math::PI * radius**2 * height
end
JavaScript ES6
static volume(radius, height) {
const volume = Math.PI * Math.pow(radius, 2) * height
console.log(volume)
}
https://jsbin.com/buwucuk/3/edit?js,console
Taking on the functionality of an existing class
Taking on the functionality of an existing class
import disney.pocahontas.plot;
public class Avatar extends pocahontas {
public plot() {
setTimeEra("Future");
setPlanet("NotEarth");
}
}
With Ruby
class MojoCup < Cup
def initialize
super
puts "Welcome to Mojo Coffee!"
end
end
With JavaScript ES6
class MojoCup extends Cup {
constructor() {
super()
console.log('Welcome to Mojo')
}
}
https://jsbin.com/buwucuk/2/edit?js,console
Superclass
Subclass
let shape = new Square()
let shape = new Triangle()
shape.draw()
A cirlce is a geometric shape with known properties.
An abstraction based on circle with some known physical properties
Based on wheel and perhaps could have an instance.
let shape = 5
let shape = new Triangle()
shape.draw()
class Square extends Shape {
String draw() {
return "Square";
}
}
class Triangle extends Shape {
String draw() {
return "Triangle";
}
}
abstract class Shape {
abstract String draw();
}
void showEveryone(final Shape shape) {
println(shape.draw());
}
int main() {
showEveryone(new Square());
showEveryone(new Triangle());
}
Keep it focussed on the project.
you can always extend it to add functionality!
or can you?....
That can be a pretty big decision
Static Type Checking
Name some static typed languages?
Dynamic languages?
Destructor methods?
Whatever you do, you must implement .draw
https://github.com/GantMan/furry_destroyer
https://github.com/IconoclastLabs/rubytrivia
Final Remarks + Survey