Hi there! Welcome back to my blog!
If you have been following along you already know the drill, but if you are new, hi! My name is Paul and I am a former student with Flatiron School who is on a journey of discovery and.. job hunting.
As the title suggests we will be talking about OOP or object-oriented programming. What is OOP? How can we use it? Is it useful? To find the answers to these questions, read along.
Disclaimer
I wanted to give a node to the 2019 Meme winner 'And I oop' Jasmine Masters.
Source
What is OOP?
Object-oriented programming aka OOP is a fundamental programming model for other programming languages such as C++ and Java.
The OOP is divided in 3 strong concepts: classes and instances, inheritance, and encapsulation.
These concepts are pretty familiar with other subjects we've tackled on this blog such as Ruby. The model behind object-oriented programming is a collection of objects. Each object representing a particular aspect of a system. Objects are a combination of functions (or as we remember them from Ruby: methods) and data. Because the object maintains its own private internal state, the system doesn't have to care what might happen inside the object.
Classes and instances
When we think about the types of objects we want to include in our system, we create abstract definitions. For example, if you are modeling a university, the professors will represent some objects. Every object is represented by some properties in common: having a name and teaching a subject. Furthermore, a professor can grade papers/projects and can introduce themselves at the beginning of every year.
That would make the Professor a class in our system. The pseudocode would look like this:
class Professor
properties
name
teaches
methods
grade(paper)
introduceSelf()
Source
We defined the Professor class with 2 properties (name and teaches) and 2 methods( grade() to grade papers and introduceSelf() to introduce themselves)
Solely by itself, a class won't do much. It will act as a blueprint for creating other objects from the same type. Each professor is created is an instance of the Professor class. The function responsible for creating instances it's called constructor. The values passed to the constructor represent the internal state we want to initialize in the new instance. Example:
class Professor
properties
name
teaches
constructor
Professor(name, teaches)
methods
grade(paper)
introduceSelf()
Source
With the constructor in place, let's create some professors!
P.S programming languages mark the signal for calling a constructor with new.
stephenie = new Professor("Stephenie", "Biology")
adam = new Professor("Adam", "History")
stephenie.teaches; // 'Biology'
stephenie.introduceSelf(); //'My name is prof. Stephenie and I will be your Biology professor.'
adam.teaches; // 'History'
adam.introduceSelf(); //'My name is prof. Adam and I will be your History professor.'
Source
The command above creates 2 new objects representing instances of the Professor class! How fun is that?!
Inheritance
Who else goes to university? Oh yes! Students. What separates students from professors, is that they can't grade (officially) papers, they don't teach any particular subject and they are usually part of a graduation year.
But, there are some aspects that are common. They also have a name and want to introduce themselves. So the class would look something like this:
class Student
properties
name
year
constructor
Student(name, year) //to be able to create instances
methods
introduceSelf()
Now we can notice that we are repeating ourselves and as respected software engineers we have to keep it D-R-Y! (Don't Repeat Yourself). If only we could find a way to write a code that represents both students and professors on the same kind of level.. Hmm. What about creating a new class called Person where we set the common properties:
`class Person
properties
name
constructor
Person(name)
methods
introduceSelf()
class Professor: extends Person
properties
teaches
constructor
Professor(name, teaches)
methods
grade(paper)
introduceSelf()
class Student : extends Person
properties
year
constructor
Student(name, year)
methods
introduceSelf()`
The Person became a superclass or parent class for both Professor and Student. The same way around, Student and Professor are subclasses or child classes of Person.
The introduceSelf() method is repeating itself in all classes as we need to differentiate the type. For example
susan = new Professor("Susan", "Drama")
susan.introduceSelf(); // 'My name is prof. Susan and I will be your Drama professor.'
john = new Student("John",2)
john.introduceSelf(); // "My name is John and I'm in my 2nd year.'
dan = new Person("Dan")
dan.introduceSelf(); // "My name is Dan."
Encapsulation
Due to its private internal state, the object's internal methods cannot be accessed by other objects. Keeping the internal state of an object private and the clear division from the public interface and private state is called encapsulation.
Let's say the students can study a certain subject only in their 2rd year or above, how can we make sure we have a system that checks the student's year?
We can pass a method in the Student objects with the logic:
class Student : extends Person
properties
year
constructor
Student(name, year)
methods
introduceSelf()
canStudyLiterature() {return this.year > 1 }
if (student.canStudyLiterature()){
//allow the student into the class
Tip!
To be sure other code can't access the object's internal state, we can use the property called private. This will throw an error if some code outside the object tries to access it.
`class Student : extends Person
properties
private year
constructor
Student(name, year)
methods
introduceSelf()
canStudyLiterature() {return this.year > 1 }
student = new Student('Dana', 1)
student.year // error: 'year' is a private property of Student
`
How fun is that?!
Summary
In this blog, we went over the basics of object-oriented programming. I hope you enjoyed this and found some parts useful. It's a learning experience for us all and if you want to learn more go ahead to mdn web docs and get more details.
Till next time, happy coding!
Top comments (2)
Hi , Scuze pt mesaj , dar azi din intamplare am Dat sa had câți Bosorogan exista in USA, și a aprut namele tau
Namele meu e Oana Bosorogan, căsătorită Popescu , din Deva
Cred ca suntem ceva rude, pt ca majoritatea Bosorogan sunt din Orăștie și am înțeles ca mai avem ceva rude la București
Eu locuiesc în PA
Nu mas fi gândit ca este cineva cu numele asta aici😊
Some comments have been hidden by the post's author - find out more