Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Blog Home

Solutions
Products
Pricing
Log InJoin for free
What is item-orientated programming? OOP defined in depth
Apr 15, 2020 - 15 min read
Erin Doherty
Object-orientated programming (OOP) is a essential programming paradigm utilized
by almost each developer in some unspecified time in the future in their career. OOP is
the maximum famous programming paradigm and is taught as the same old manner to code
for maximum of a programmers educational career.
Today we can spoil down the fundamentals of what makes a application item-orientated so
you can begin to make use of this paradigm on your personal initiatives and interviews.
Here’s what is going to be covered:

 What is Object-orientated programming?
 Building blocks of OOP
 Four concepts of OOP
 What to examine next

What is Object Oriented Programming?


Object Oriented programming (OOP) is a programming paradigm that is based at
the idea of lessons and gadgets. It is used to shape a software program application into easy,
reusable portions of code blueprints (normally known as lessons), that are used to
create person times of gadgets. There are many item-orientated programming
languages which includes JavaScript, C++, Java, and Python.
A magnificence is an summary blueprint used to create greater particular, concrete gadgets.
Classes often constitute huge categories, like Car or Dog that share attributes.
These lessons outline what attributes an example of this kind will have,
like color, however now no longer the cost of these attributes for a particular item.
Classes also can include capabilities, known as techniques to be had most
effective to gadgets of that kind. These capabilities are described in
the magnificence and carry out a few movement useful to that particular kind of item.
For instance, our Car magnificence might also additionally have
a technique repaint that adjustments the color characteristic of our automobile.
This feature is most effective useful to gadgets of kind Car, so we claim it in
the Car magnificence hence making it a technique.
Class templates are used as a blueprint to create person gadgets.
These constitute particular examples of the summary magnificence,
like myCar or goldenRetriever. Each item will have specific values to the houses described in
the magnificence.
For instance, say we created a magnificence, Car, to include all of
the houses a automobile must have, color, brand, and model. We then create an example of
a Car kind item, myCar to symbolize my particular automobile.
We should then set the cost of the houses described withinside the magnificence to
explain my automobile, with out affecting different gadgets or the magnificence template.
We can then reuse this magnificence to symbolize any range of cars.
Class blueprint being used to create Car kind gadgets, myCar and helensCar
Benefits of OOP

 OOP fashions complicated matters as reproducible, easy systems
 Reusable, OOP gadgets may be used throughout programs
 Allows for magnificence-particular conduct via polymorphism
 Easier to debug, lessons often include all relevant data to them
 Secure, protects data via encapsulation

How to shape OOP programs


Let’s take a actual international problem, and conceptually layout an OOP software
program application.
Imagine going for walks a canine sitting camp, with masses of pets, and you need
to maintain music of the names, ages, and days attended for every pet.
How might you layout easy, reusable software program to model the puppies?
With masses of puppies, it might be inefficient to write down specific code for every canine.
Below we see what that would appear like with gadgets rufus and fluffy.
//Object of 1 person canine
var rufus = {
    name: "Rufus",
    birthday: "2/1/2017",
    age: feature() {
        go back Date.now() - this.birthday;
    },
    attendance: 0
}
//Object of 2nd person canine
var fluffy = {
    name: "Fluffy",
    birthday: "1/12/2019",
    age: feature() {
        go back Date.now() - this.birthday;
    },
    attendance: 0
}
As you may see above, there is lots of duplicated code among each gadgets.
The age() feature seems in every item. Since we need the equal data for every canine, we are
able to use gadgets and lessons instead.
Grouping associated data collectively to shape a magnificence shape makes the code shorter
and less difficult to maintain.
In the dogsitting instance, here’s how a programmer should reflect onconsideration
on organizing an OOP:

1. Create a figure magnificence for all puppies as a blueprint of data and behaviors


(techniques) that every one puppies will have, irrespective of kind.
2. Create baby lessons to constitute specific subcategories
of canine below the regular figure blueprint.
3. Add specific attributes and behaviors to the kid lessons to symbolize differences
4. Create gadgets from the kid magnificence that constitute puppies inside that
subgroup

The diagram under represents a way to layout an OOP application: grouping


the associated information and behaviors collectively to shape a easy template
then growing subgroups for specialised information and conduct.
The Dog magnificence is a regular template, containing most
effective the shape approximately information and behaviors not unusualplace to all puppies.
We then create baby lessons of Dog, HerdingDog and TrackingDog. These have the inherited
behaviors of Dog (bark()) however additionally conduct specific to puppies of that subtype.
Finally,we create gadgets of the HerdingDog kind to
symbolize the person puppies Fluffy and Maisel.
We also can create gadgets like Rufus that in
shape below the huge magnificence of Dog however do now no longer in
shape below either HerdingDog or TrackingDog.
Building blocks of OOP
Next, we’ll take a deeper examine every of the essential constructing blocks of an
OOP application used above:

 Classes
 Objects
 Methods
 Attributes

Classes
In a nutshell, lessons are essentially user described information sorts. Classes are in
which we create a blueprint for the shape of techniques and attributes. Individual gadgets are
instantiated, or comprised of this blueprint.
Classes include fields for attributes, and techniques for behaviors. In
our Dog magnificence instance, attributes include name & birthday, at the same time
as techniques include bark() and updateAttendance().
Here’s a code snippet demonstrating a way to application a Dog magnificence the usage
of the JavaScript language.
magnificence Dog {
    constructor(name, birthday) {
        this.name = name;
        this.birthday = birthday;
    }
    //Declare non-public variables
    _attendance = 0;
    getAge() {
        //Getter
        go back this.calcAge();
    }
    calcAge() {
        //calculate age the usage of state-of-the-art date and birthday
        go back Date.now() - this.birthday;
    }
    bark() {
        go back console.log("Woof!");
    }
    updateAttendance() {
        //{add|upload} an afternoon to the canine's attendance days on the petsitters
        this._attendance++;
    }
}
Remember the magnificence is a template for modeling a canine, and an item is instantiated
from the magnificence representing an person actual international thing.
Enjoying the article? Scroll down to signal up for our free, bi-month-to-month newsletter.
Objects
Of path OOP consists of gadgets! Objects are times of lessons created
with particular information, for instance withinside the code snippet under Rufus is
an example of the Dog magnificence.
magnificence Dog {
    constructor(name, birthday) {
        this.name = name;
        this.birthday = birthday;
    }
    //Declare non-public variables
    _attendance = 0;
    getAge() {
        //Getter
        go back this.calcAge();
    }
    calcAge() {
        //calculate age the usage of state-of-the-art date and birthday
        go back Date.now() - this.birthday;
    }
    bark() {
        go back console.log("Woof!");
    }
    updateAttendance() {
        //{add|upload} an afternoon to the canine's attendance days on the petsitters
        this._attendance++;
    }
}
//instantiate a brand new item of the Dog magnificence, and person canine named Rufus
const rufus = new Dog("Rufus", "2/1/2017");
When the brand new magnificence Dog is known as:

 A new item is created named rufus


 The constructor runs name & birthday arguments, and assigns values

Programming vocabulary:
In JavaScript gadgets are a kind of variable. This might also
additionally reason confusion, due to the fact gadgets also can be declared with
out a magnificence template in JavaScript, as proven at the start.
Objects have states and behaviors. State is described with the aid of
using [removed]PHN2ZyB3aWR0aD0iNTIzMCIgaGVpZ2h0PSIyNjgwIiB4bWxucz0iaHR0cDovL3
d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIvPg=="
style='width:24pt;height:24pt;visibility:visible;mso-wrap-style:square; mso-left-percent:-
10001;mso-pinnacle-percent:-10001;mso-position-horizontal:absolute; mso-position-
horizontal-relative:char;mso-position-vertical:absolute; mso-position-vertical-
relative:line;mso-left-percent:-10001;mso-pinnacle-percent:-10001; v-text-anchor:pinnacle'
o:gfxdata="UEsDBBQABgAIAAAAIQC75UiUBQEAAB4CAAATAAAAW0NvbnRlbnRfVHlwZXN
dLnhtbKSRvU7DMBSF
dyTewfKKEqcMCKEmHfgZgaE8wMW+SSwc27JvS/v23KTJgkoXFsu+P+c7Ol5vDoMTe0zZBl/
LVVlJ
gV4HY31Xy4/tS3EvRSbwBlzwWMsjZrlprq/W22PELHjb51r2RPFBqax7HCCXIaLnThvSAMTP1K
kI
+gs6VLdVdad08ISeCho1ZLN+whZ2jsTzgcsnJwldluLxNDiyagkxOquB2Knae/OLUsyEkjenmdzb
mG/YhlRnCWPnb8C898bRJGtQvEOiVxjYhtLOxs8AySiT4JuDystlVV4WPeM6tK3VaILeDZxIOSs
u
ti/jidNGNZ3/J08yC1dNv9v8AAAA//8DAFBLAwQUAAYACAAAACEArTA/8cEAAAAyAQAACwA
AAF9y
ZWxzLy5yZWxzhI/NCsIwEITvgu8Q9m7TehCRpr2I4FX0AdZk2wbbJGTj39ubi6AgeJtl2G9m6vYx
jeJGka13CqqiBEFOe2Ndr+B03C3WIDihMzh6RwqexNA281l9oBFTfuLBBhaZ4ljBkFLYSMl6oA
m5
8IFcdjofJ0z5jL0MqC/Yk1yW5UrGTwY0X0yxNwri3lQgjs+Qk/+zfddZTVuvrxO59CNCmoj3vCwj
MfaUFOjRhrPHaN4Wv0VV5OYgm1p+LW1eAAAA//8DAFBLAwQUAAYACAAAACEA0HWkIWA
DAAALBwAA
HwAAAGNsaXBib2FyZC9kcmF3aW5ncy9kcmF3aW5nMS54bWykVV1z4jYUfe9M/4NHr63jDwzB
3nV2
goGEmUCZsNudyZuQha1UllxJGEin/73XMiwk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y1YJoWqwrdaRdnyYOUOqNw2rHh88DcD7xAK7gA4MPspqS1ZQ
MruCr
AbSL7Ki/four+cXhQSeZ5ISUy8k9QLTKCSNQtIsJM1C0iokLd/TZ+LwHUYdh/teceQNPSw/Is/
XFvb3
m+1/AQAA//8DAFBLAwQUAAYACAAAACEAnGZGQbsAAAAkAQAAKgAAAGNsaXBib2FyZC9
kcmF3aW5n
cy9fcmVscy9kcmF3aW5nMS54bWwucmVsc4SPzQrCMBCE74LvEPZu0noQkSa9iNCr1AcIyTYt
Nj8k
UezbG+hFQfCyMLPsN7NN+7IzeWJMk3ccaloBQae8npzhcOsvuyOQlKXTcvYOOSyYoBXbTXP
FWeZy
lMYpJFIoLnEYcw4nxpIa0cpEfUBXNoOPVuYio2FBqrs0yPZVdWDxkwHii0k6zSF2ugbSL6Ek/2f7
YZgUnr16WHT5RwTLpRcWoIwGMwdKV2edNS1dgYmGff0m3gAAAP//AwBQSwECLQAUAAYA
CAAAACEA
u+VIlAUBAAAeAgAAEwAAAAAAAAAAAAAAAAAAAAAAW0NvbnRlbnRfVHlwZXNdLnhtbFBL
AQItABQA
BgAIAAAAIQCtMD/xwQAAADIBAAALAAAAAAAAAAAAAAAAADYBAABfcmVscy8ucmVsc1B
LAQItABQA
BgAIAAAAIQDQdaQhYAMAAAsHAAAfAAAAAAAAAAAAAAAAACACAABjbGlwYm9hcmQvZ
HJhd2luZ3Mv
ZHJhd2luZzEueG1sUEsBAi0AFAAGAAgAAAAhALY7BCJUBgAACxoAABoAAAAAAAAAAAA
AAAAAvQUA
AGNsaXBib2FyZC90aGVtZS90aGVtZTEueG1sUEsBAi0AFAAGAAgAAAAhAJxmRkG7AAAAJ
AEAACoA
AAAAAAAAAAAAAAAASQwAAGNsaXBib2FyZC9kcmF3aW5ncy9fcmVscy9kcmF3aW5nMS54
bWwucmVs c1BLBQYAAAAABQAFAGcBAABMDQAAAAA= " filled="f" stroked="f">
Encapsulation provides security. Attributes and techniques may be set to non-public, so that
they can’t be accessed out of doors the magnificence. To
get data approximately information in an item, public techniques & houses are used to get
entry to or replace information.
This provides a layer of security, in which the developer chooses what information may
be visible on an item with the aid of using exposing
that information via public techniques withinside the magnificence definition.
Within lessons, maximum programming languages have public, protected, and
personal sections. Public is the restricted choice of techniques to be had to the out of
doors international, or different lessons in the application. Protected is most
effective handy to baby lessons.
Private code can most effective be accessed from inside that magnificence.
To pass returned to our canine/proprietor instance, encapsulation is
right so proprietors can’t get entry to non-
public data approximately different people’s puppies.
Note: JavaScript has non-public and guarded houses and techniques. Protected Fields are
prefixed with a _ ; non-public fields are prefixed with a #. Protected fields are inherited, non-
public ones aren’t.
//Parent magnificence Dog
magnificence Dog{
    //Declare protected (non-public) fields
    _attendance = 0;
    constructor(namee, birthday) {
        this.name = name;
        this.birthday = birthday;
    }
    getAge() {
        //Getter
        go back this.calcAge();
    }
    calcAge() {
        //calculate age the usage of state-of-the-art date and birthday
        go back this.calcAge();
    }
    bark() {
        go back console.log("Woof!");
    }
    updateAttendance() {
        //{add|upload} an afternoon to the canine's attendance days on the petsitters
        this._attendance++;
    }
}
//instantiate a brand new example of Dog magnificence, an person canine named Rufus
const rufus = new Dog("Rufus", "2/1/2017");
//use getter technique to calculate Rufus' age
rufus.getAge();
Consider the getAge() technique in our instance code, the calculation info are
hidden internal the Dog magnificence. The rufus item makes use of the getAge() technique to
calculate Rufus’s age.
Encapsulating & updating
[removed]PHN2ZyB3aWR0aD0iNDg1MCIgaGVpZ2h0PSIyNzAwIiB4bWxucz0iaHR0cDovL3d3d
y53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIvPg=="
style='width:24pt;height:24pt;visibility:visible;mso-wrap-style:square; mso-left-percent:-
10001;mso-pinnacle-percent:-10001;mso-position-horizontal:absolute; mso-position-
horizontal-relative:char;mso-position-vertical:absolute; mso-position-vertical-
relative:line;mso-left-percent:-10001;mso-pinnacle-percent:-10001; v-text-anchor:pinnacle'
o:gfxdata="UEsDBBQABgAIAAAAIQC75UiUBQ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AAAqAAAAY2xpcGJvYXJkL2Ry
YXdpbmdz
L19yZWxzL2RyYXdpbmcxLnhtbC5yZWxzhI/NCsIwEITvgu8Q9m7SehCRJr2I0KvUBwjJNi02Py
RR
7Nsb6EVB8LIws+w3s037sjN5YkyTdxxqWgFBp7yenOFw6y+7I5CUpdNy9g45LJigFdtNc8VZ5n
KU xikkUigucRhzDifGkhrRykR9QFc2g49W5iKjYUGquzTI9lV1YPGTAeKLSTrNIXa6BtIvoST/Z/th
mBSevXpYdPlHBMulFxagjAYzB0pXZ501LV2BiYZ9/SbeAAAA//8DAFBLAQItABQABgAIAAAAI
QC7
5UiUBQEAAB4CAAATAAAAAAAAAAAAAAAAAAAAAABbQ29udGVudF9UeXBlc10ueG1sUEs
BAi0AFAAG
AAgAAAAhAK0wP/HBAAAAMgEAAAsAAAAAAAAAAAAAAAAANgEAAF9yZWxzLy5yZWxzU
EsBAi0AFAAG
AAgAAAAhAIM+Ol1fAwAACwcAAB8AAAAAAAAAAAAAAAAAIAIAAGNsaXBib2FyZC9kcmF3
aW5ncy9k
cmF3aW5nMS54bWxQSwECLQAUAAYACAAAACEAtjsEIlQGAAALGgAAGgAAAAAAAAAAA
AAAAAC8BQAA
Y2xpcGJvYXJkL3RoZW1lL3RoZW1lMS54bWxQSwECLQAUAAYACAAAACEAnGZGQbsAAAA
kAQAAKgAA
AAAAAAAAAAAAAABIDAAAY2xpcGJvYXJkL2RyYXdpbmdzL19yZWxzL2RyYXdpbmcxLnhtb
C5yZWxz UEsFBgAAAAAFAAUAZwEAAEsNAAAAAA== " filled="f" stroked="f">
Abstraction additionally serves an vital security role. By most effective showing decided
on portions of information, and most effective permitting information to
be accessed via lessons and changed via techniques, we guard the information from
exposure. To keep with the automobile instance, you wouldn’t need an open fueloline tank at
the same time as using a automobile.

The advantages of abstraction are summarized under:

 Simple, excessive degree user interfaces
 Complex code is hidden
 Security
 Easier software program maintenance
 Code updates hardly ever change abstraction

Polymorphism

Polymorphism approach designing gadgets to share behaviors. Using


inheritance, gadgets can override shared figure behaviors, with particular baby behaviors.
Polymorphism lets in the equal technique to execute specific behaviors
in ways: technique overriding and technique overloading.

Method Overriding

Runtime polymorphism makes use of technique overriding. In technique overriding,


a baby magnificence can offer a specific implementation than its figure magnificence. In
our canine instance, we might also additionally need to give TrackingDog a particular kind
of bark specific than the regular canine magnificence.

Method overriding should create a bark() technique withinside the baby magnificence that


overrides the bark() technique withinside the figure Dog magnificence.

//Parent magnificence Dog

magnificence Dog{

    //Declare protected (non-public) fields

    _attendance = 0;

    constructor(namee, birthday) {

        this.name = name;

        this.birthday = birthday;

    }
    getAge() {

        //Getter

        go back this.calcAge();

    }

    calcAge() {

        //calculate age the usage of state-of-the-art date and birthday

        go back this.calcAge();

    }

    bark() {

        go back console.log("Woof!");

    }

    updateAttendance() {

        //{add|upload} an afternoon to the canine's attendance days on the petsitters

        this._attendance++;

    }

//Child magnificence TrackingDog, inherits from figure

magnificence TrackingDog extends Dog {

    constructor(name, birthday)

        super(name);

        super(birthday);

    }

    music() {

        //{additional|extra} technique for TrackingDog baby magnificence

        go back console.log("Searching...")

    }

    bark() {
        go back console.log("Found it!");

    }

//instantiate a brand new TrackingDog item

const duke = new TrackingDog("Duke", "1/12/2019");

duke.bark(); //returns "Found it!"

TrackingDog's overriding the bark() technique

Method Overloading

Compile Time polymorphism makes use of technique overloading. Methods


or capabilities might also additionally have the equal name, however a specific range of
parameters exceeded into the technique call. Different effects might also
additionally occur relying at the range of parameters exceeded in.

//Parent magnificence Dog

magnificence Dog{

    //Declare protected (non-public) fields

    _attendance = 0;

    constructor(namee, birthday) {

        this.name = name;

        this.birthday = birthday;

    }

    getAge() {

        //Getter

        go back this.calcAge();

    }

    calcAge() {

        //calculate age the usage of state-of-the-art date and birthday

        go back this.calcAge();

    }

    bark() {
        go back console.log("Woof!");

    }

    updateAttendance() {

        //{add|upload} an afternoon to the canine's attendance days on the petsitters

        this._attendance++;

    }

    updateAttendance(x) {

        //provides a couple of to the canine's attendance days on the petsitters

        this._attendance = this._attendance + x;

    }

//instantiate a brand new example of Dog magnificence, an person canine named Rufus

const rufus = new Dog("Rufus", "2/1/2017");

rufus.updateAttendance(); //attendance = 1

rufus.updateAttendance(four); // attendance = 5

In this code instance, if no parameters are exceeded into the updateAttendance() technique.


One day is delivered to the count. If a parameter is exceeded in updateAttendance(four),
then four is exceeded into the x parameter in updateAttendance(x), and four days
are delivered to the count.

The advantages of Polymorphism are:

 Objects of various sorts can be exceeded via the equal interface


 Method overriding
 Method overloading

Conclusion

Object Oriented programming calls for thinking approximately the shape of this


system and making plans at the start of coding. Looking at how to interrupt up
the necessities into easy, reusable lessons that may be used to blueprint times of gadgets.
Overall, imposing OOP lets in for higher information systems and reusability, saving
time withinside the lengthy run.
If you’d want to take a deep dive into OOP, Educative has publications for OOP in:

 Java
 JavaScript
 Python
 C++
 C#

These publications are text-primarily based totally with in-browser coding environments so


that you can examine even quicker and greater efficiently. No set-up required, simply get
in and begin coding.

Happy learning!

Continue studying approximately Object Oriented Programming

 S.O.L.I.D. Principles of Object-Oriented Programming in C#


 Exploring item-orientated programming standards with Java
 How to Use Object-Oriented Programming in Python

WRITTEN BYErin Doherty

Join a network of greater than 1 million readers. A free, bi-month-to-month electronic


mail with a roundup of Educative's pinnacle articles and coding tips.

Top of Form

Subscribe

Bottom of Form

Learn in-call for tech abilities in 1/2 of the time

SOLUTIONS

For Enterprise

For Individuals

For HR & Recruiting

For Bootcamps

PRODUCTS

Educative Learning

Educative Onboarding
Educative Skill Assessments

Educative Projects

PRICING

For Enterprise

For Individuals

Free Trial

LEGAL

Privacy Policy

Cookie Settings

Terms of Service

Business Terms of Service

CONTRIBUTE

Become an Author

Become an Affiliate

Become a Contributor

RESOURCES

Educative Blog

EM Hub

Educative Sessions

Educative Answers

ABOUT US

Our Team

Careers

Hiring

Frequently Asked Questions

Contact Us

Press
MORE

GitHub Students Scholarship

Course Catalog

Early Access Courses

Earn Referral Credits

CodingInterview.com

Copyright ©2022 Educative, Inc. All rights reserved.

You might also like