Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4605

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T01:32:09+00:00 2024-11-27T01:32:09+00:00

And I OOP (Object-oriented Programming)

  • 61k

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.
Image descriptionSource

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.'  
Enter fullscreen mode Exit fullscreen mode

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." 
Enter fullscreen mode Exit fullscreen mode

Source

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 
Enter fullscreen mode Exit fullscreen mode

Source

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!

javascriptoopprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.