Object oriented programming

object oriented programming (OOP) is a programming pardigm where data is dealt with in a similar fashion to objects in a real world. A simple example would be cars. Each car has similar qualities like color, model, make, these are called properties. Each car also functions in a similar way and can do things like start, brake, accelerate, steer, etc. these are know as methods but can also be referred to as functions.

Using the information above here is a simple example of what a car object would look like in javascript. Creating an object using this method is called writing an object literal

var civic = {
  make: 'Honda',
  model: 'Civic',
  color: 'blue',
  gallonsOfGas: 5,
  speed: 0,

  accelerate: function(){
    this.speed += 1;
    this.gallonsOfGas -= 0.01;
  },

  brake: function(){
    this.speed -= -1;
  }
}

Another way of creating an object is using the new keyword to first create or instantiate the object, then adding values using dot notation or passing the key like so civic['model'] = 'Civic';

var civic = new Object();

civic.color = 'red';
civic['model'] = 'Civic'; // alternate syntax

Writing this information out can be tedious, what if we had a quick way to enter all of this information, and let the computer take care of constructing the object? Fortunately we do! This is what's known as a constructor function.

function Car(make, model, color, gallonsOfGas){
  this.make = make;
  this.model = model;
  this.color = color;
  this.gallonsOfGas = gallonsOfGas;
  this.speed = 0;
  this.accelerate = function(){
    this.speed += 1;
    this.gallonsOfGas -= 0.01;
  },

  this.brake = function(){
    this.speed -= -1;
  }
}

Then we can easily create a new Car using the following.

var corvette = new Car('Chevrolet', 'Corvette', 'red', '0');

Finally, we access these properties using dot notation, and we access the methods the same way, but to use the method we need to call them by adding () to the end.

corvette.speed; // returns 0
corvette.accelerate(); // returns undefined, but adds 1 to speed
corvette.speed // now returns 1

Posted Oct 23, 2017, 4:22 AM UTC · edited

2310
smthamazing•

A simple example would be cars. Each car has similar qualities like color, model, make, these are called properties. Each car also functions in a similar way and can do things like start, brake, accelerate, steer, etc. these are know as methods but can also be referred to as functions.

Using a purely functional language, like Haskell, I can also model this structure using a data type with nested fields and writing functions for working with it. Including a convenient constructor.

Is my understanding correct?

  • In FP you have immutable instances of types that hold data, and separate functions that work with them. Logic and data are separate, data is immutable.
  • In OOP you have (often mutable) instances of types that hold both data and functions that work with them (which are called methods). Logic and data are combined into an object, data is often mutable.

Also, as far as I know, Smalltalk is the first OOP language and it had no notion of a class or method, instead having objects and something called "message passing". If even the notion of a method is not necessary for a language to be OOP, what differences from FP remain, apart from mutability? Or is mutability the only factor that fundamentally distinguishes OOP paradigm from FP?

I know that paradigms can be combined in the same codebase, but as I learn more OOP and FP, I start to struggle to really differentiate them. I would appreciate an explanation.

7
JackLegJosh•

Upvote for you, friend. I would love to see an ELI5 about functional programming in practice, not as an esoteric concept.

1
[deleted]•

[deleted]

1
smthamazing•

This is interesting. But then I still don't understand what OOP is.

  • It's not about nested structures, because pure functional languages have nested structures.
  • It's not constructors, because pure functional languages can have constructors.
  • It's not mutability, because, as you say, the data can be updated immutably.
  • It's not classes and methods, because Smalltalk doesn't have them, and yet it is considered an OOP language.

The only remaining thing is that the data and behavior are represented as parts of a single entity, an "object" (often, but not necessarily, an instance of some class/type).

Is that it? Fundamentally, is combining data+logic the only unique part of OOP as a paradigm?

1
[deleted]•

[deleted]

1
leaOfCat•

Might want to check your break function.

Otherwise pretty good explanation of OOP

3
xnull8•

Was about to point that out. Also gallonsOfGas should be a number in the example and not a string.

1
ReignNFire•

This is great! Thanks!

2
theginger3469•

When do you decide to define a new class instead of continuing to add methods to the same class?

2
cyphrrrOP•

a class is like the type of object, in the example above the class would be Car, if you need to create an object that can't be modeled using the Car class e.g. a violin then you would need to create a new class.

1
CodeTinkerer•

From other sources, I think people would say OOP must have inheritance, otherwise, it's not considered OOP. They would call it object based programming. To be fair, I hardly ever use inheritance (I do use interfaces in Java, however).

2
smthamazing•

If I understand correctly, even classes are not necessary for a language to be OOP.

1