Object-oriented Programming in 7 minutes | Mosh

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Good little refresher, makes me realize I should probably review these concepts in more detail

👍︎︎ 5 👤︎︎ u/pro_intern 📅︎︎ Nov 28 2018 🗫︎ replies

I wish there were more examples / discussions of non-trivial OOP code snippets.

Everyone groks the nice animal / car / employee object examples but in my experience once you get into real world production code examples the concrete-ness of objects used in tutorials like these gets lost and you end up with ControllerServices and ControllerManagers and AbstractThings.

Its like a kind of "homeopathic-programming by abstraction" where the more abstract we make the objects the less information they provide about the domain of the problem you're trying to solve. Like its easy to reason about an Employee object... Just by its name alone you can probably guess the properties it has and operations it can perform. But in the world of AbstractThings its impossible to reason about objects in the same blackbox manner that you can with more concrete objects.

/rant

👍︎︎ 5 👤︎︎ u/[deleted] 📅︎︎ Nov 29 2018 🗫︎ replies

I love his accent, seems french. Thanks for sharing

👍︎︎ 2 👤︎︎ u/DieEeneGast 📅︎︎ Nov 28 2018 🗫︎ replies

The encapsulation part was expained beautifully. so, after three years of procedural programming, today I've made the aha-moment thanks to this.

I was looking forward to an explanation in the same style for inheritance in a practical JS case, but I am happy with having grasped 100% the first part. (I'm a full Stack Designer for now and slowly dealing with more and more complex JS)

Also I'm still wondering about how those variables arrive to the object in the first encapsulated example.. via... Params? hope not.

👍︎︎ 2 👤︎︎ u/waldito 📅︎︎ Nov 28 2018 🗫︎ replies

Not sure if I agree with the quote: "The best functions are those with no parameters." - Uncle Bob Martin.

Functions, by their very definition, require inputs. A function takes in an input, does some calculation within itself, and returns an output. A function cannot perform any dynamic calculations if it cannot take inputs. So therefore these are not functions but indeed methods.

Granted, this is OOP and not functional programming and so the data these methods receive are from properties assigned within the object, whether they are assigned using constructors or other methods. However one of the problems attributed to OOP is tracking state because it can be difficult to determine what a method returns. In functional programming a function should always return the same output if given the same inputs.

Otherwise, good video. Succinct and addresses core OOP concepts.

👍︎︎ 2 👤︎︎ u/bmy1978 📅︎︎ Nov 29 2018 🗫︎ replies

informative

👍︎︎ 1 👤︎︎ u/lizajhon306 📅︎︎ Nov 28 2018 🗫︎ replies

Cool vid

👍︎︎ 1 👤︎︎ u/slinkywafflepants 📅︎︎ Nov 28 2018 🗫︎ replies

Super easy and practical examples. It's one of the best explanations I've seen on that topic.

👍︎︎ 1 👤︎︎ u/haukebr 📅︎︎ Nov 28 2018 🗫︎ replies

Anyone notice this?

let baseSalary = 30_000;

This can't possibly be valid syntax right?

👍︎︎ 1 👤︎︎ u/LegenKiller666 📅︎︎ Nov 29 2018 🗫︎ replies
Captions
[Music] a popular interview question concerns the four core concepts in object-oriented programming this concepts are encapsulation abstraction inheritance and polymorphism let's look at each of these concepts before object-oriented programming we had procedure of programming that divided a program into a set of functions so we have data stored in a bunch of variables and functions that operate on the data this style of programming is very simple and straightforward often it's what you learn as part of your first programming subject at a university but as your programs grow it will end up with a bunch of functions that are all over the place you might find yourself copying and pasting lines of code over and over you make a change to one function and then several other functions break that's what we call spaghetti code there is so much interdependence e between all these functions it becomes problematic object-oriented programming came to solve this problem in object-oriented programming we combine a group of related variables and functions into a unit we call that unit an object we refer to these variables as properties and the functions as methods here's an example think of a car a car is an object with properties such as make model and color and methods like start stop and move now you might say what marche we don't have cars in our programs give me a real programming example ok think of the local storage object in your browser's every browser has a local storage object that allows you to store data locally this local storage object has a property like length which returns the number of objects in the storage and metals like set item and remove item so in object-oriented programming we group related variables and functions that operate on them into objects and this is what we call encapsulation let me show you an example of this in action so here we have three variables base salary over time and rate below these we have a function to calculate the wage for an employee we refer to this kind of implementation as procedural so we have variables on one side and functions on the other side they're hard decoupled now let's take a look at the object-oriented way to solve this problem we can have an employee object with three properties a salary over time and rate and a method called get wage now why is this better well first of all look at the get wage function this function has no parameters in contrast in a procedural example our get wage function has three parameters the reason in this implementation we don't have any parameters is because all these parameters are actually modeled as properties of this object all these properties and the get wage function they are highly related so they are part of one unit so one of the symptoms of procedural code is functions with so many parameters when you write code in an object-oriented way your functions end up having fewer and fewer parameters as Uncle Bob says the best functions are those with no parameters the fewer the number of parameters the easier it is to use and maintain that function so that's encapsulation now let's look at abstraction think of a DVD player as an object this DVD player has a complex logic board on the inside and a few buttons on the outside that you interact with you simply press the play button and you don't care what happens on the inside all that complexity is hidden from you this is abstraction in practice we can use the same technique in our objects so we can hide some of the properties and methods from the outside and this gives us a couple of benefits first is that we'll make the interface of those objects simpler using an understanding an object with a few properties and methods is easier than an object with several properties and methods the second benefit is that it helps us reduce the impact of change let's imagine that tomorrow we change these inner or private methods these changes will leak to the outside because we don't have any code that touches these methods outside of their containing object we may delete a method or change its parameters but none of these changes will impact the rest of the applications code so with abstraction we reduce the impact of change now the third core concept in object-oriented programming inheritance inheritance is a mechanism that allows you to eliminate redundant code here's an example think of HTML elements like text boxes drop-down lists checkboxes and so on all these elements have a few things in common they should have properties like hidden and inner HTML and metals like click and focus instead of redefining all these properties and methods for every type of HTML element we can define them once in a generic object call it HTML element and have other objects inherit these properties and methods so inheritance helps us eliminate redundant code and finally polymorphism poly means many more means form so polymorphism means many forms in object-oriented programming polymorphism is a technique that allows you to get rid of long ethanol's or switch and case statements so back to our HTML elements example all these objects should have the ability to be rendered on a page but the way each element is rendered is different from the others if you want to render multiple HTML elements in a procedural way our code would probably look like this but with object orientation we can implement a render method in each of these objects and the render method will behave differently depending on the type of the object you're referencing so we can get rid of this nasty switch and case and use one line of code like this you will see that later in the course so here are the benefits of object oriented programming using encapsulation we group related variables and functions together and this way we can reduce complexity now we can reuse this object and do from parts of a program or in different programs with abstraction we hide the details and the complexity and show only the essentials this technique reduces complexity and also isolates the impact of changes in the code with inheritance we can eliminate redundant code and with polymorphism we can refactor ugly switch case statements well hello it's me mash again I wanted to say thank you very much for watching this tutorial to the end I hope you learned a lot please share and like this video to support me if you want to learn more about the object-oriented programming as I told you before I have a course called object-oriented programming in JavaScript if you want to learn more click on the link in the video description and enroll in the course if not that's perfectly fine make sure to subscribe to my channel because I upload new videos every week thank you and have a great day
Info
Channel: Programming with Mosh
Views: 2,382,015
Rating: 4.9171376 out of 5
Keywords: oop, object, object oriented programming, object-oriented programming, inheritance, encapsulation, abstraction, polymorphism, programming with mosh, code with mosh, oop concepts, oops concepts
Id: pTB0EiLXUC8
Channel Id: undefined
Length: 7min 34sec (454 seconds)
Published: Thu Mar 29 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.