Object Oriented Programming Mechatronics ND3 - 071659
Object Oriented Programming Mechatronics ND3 - 071659
Today we will break down the basics of what makes a program object-
oriented so that you can start to utilize this paradigm in your own projects and
interviews.
Classes can also contain functions, called methods available only to objects
of that type. These functions are defined within the class and perform some
action helpful to that specific type of object.
For example, our Car class may have a method repaint that changes
the color attribute of our car. This function is only helpful to objects of type Car, so
we declare it within the Car class thus making it a method.
Each object can have unique values to the properties defined in the class.
For example, say we created a class, Car, to contain all the properties a car must
have, color, brand, and model. We then create an instance of a Car type
object, myCar to represent my specific car.
We could then set the value of the properties defined in the class to describe my
car, without affecting other objects or the class template.
Class blueprint being used to create two Car type objects, myCar and helensCar
Benefits of OOP
Let’s take a real world problem, and conceptually design an OOP software
program.
Imagine running a dog sitting camp, with hundreds of pets, and you have to
keep track of the names, ages, and days attended for each pet. How would
you design simple, reusable software to model the dogs?
With hundreds of dogs, it would be inefficient to write unique code for each
dog. Below we see what that might look like with objects rufus and fluffy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Object of one individual dog
var rufus = {
name: "Rufus",
birthday: "2/1/2017",
age: function() {
return Date.now() - this.birthday;
},
attendance: 0
}
//Object of second individual dog
var fluffy = {
name: "Fluffy",
birthday: "1/12/2019",
age: function() {
return Date.now() - this.birthday;
},
attendance: 0
}
As you can see above, there is a lot of duplicated code between both objects.
The age() function appears in each object. Since we want the same information
for each dog, we can use objects and classes instead.
4. Create objects from the child class that represent dogs within that
subgroup
The diagram below represents how to design an OOP program: grouping the
related data and behaviors together to form a simple template then creating
subgroups for specialized data and behavior.
The Dog class is a generic template, containing only the structure about data
and behaviors common to all dogs.
We then create two child classes of Dog, HerdingDog and TrackingDog. These have
the inherited behaviors of Dog (bark()) but also behavior unique to dogs of that
subtype.
We can also create objects like Rufus that fit under the broad class of Dog but
do not fit under either HerdingDog or TrackingDog.
Classes
Objects
Methods
Attributes
Classes
In a nutshell, classes are essentially user defined data types. Classes are
where we create a blueprint for the structure of methods and attributes.
Individual objects are instantiated, or created from this blueprint.
Classes contain fields for attributes, and methods for behaviors. In
our Dog class example, attributes include name & birthday, while methods
include bark() and updateAttendance().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Dog {
constructor(name, birthday) {
this.name = name;
this.birthday = birthday;
}
//Declare private variables
_attendance = 0;
getAge() {
//Getter
return this.calcAge();
}
calcAge() {
//calculate age using today's date and birthday
return Date.now() - this.birthday;
}
bark() {
return console.log("Woof!");
}
updateAttendance() {
//add a day to the dog's attendance days at the petsitters
this._attendance++;
}
}
Objects
Of course OOP includes objects! Objects are instances of classes created
with specific data, for example in the code snippet below Rufus is an instance
of the Dog class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Dog {
constructor(name, birthday) {
this.name = name;
this.birthday = birthday;
}
//Declare private variables
_attendance = 0;
getAge() {
//Getter
return this.calcAge();
}
calcAge() {
//calculate age using today's date and birthday
return Date.now() - this.birthday;
}
bark() {
return console.log("Woof!");
}
updateAttendance() {
//add a day to the dog's attendance days at the petsitters
this._attendance++;
}
}
//instantiate a new object of the Dog class, and individual dog named Rufus
const rufus = new Dog("Rufus", "2/1/2017");
The constructor runs name & birthday arguments, and assigns values
Programming vocabulary:
In JavaScript objects are a type of variable. This may cause confusion, because
objects can also be declared without a class template in JavaScript, as shown at
the beginning.
Objects have states and behaviors. State is defined by data: things like names,
birthday, and other information you’d want to store about a dog. Behaviors are
methods, the object can undertake.
What is Information
N/A Actions Example
it? Contained
Rufus,
Objects Instance State, Data Methods
Fluffy
Attributes
Methods
When individual objects are instantiated, these objects can call the methods
defined in the class. In the code snippet below, the bark method is defined
in Dog class, and the bark() method is called on the Rufus object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Dog {
//Declare protected (private) fields
_attendance = 0;
constructor(name, birthday) {
this.namee = name;
this.birthday = birthday;
}
getAge() {
//Getter
return this.calcAge();
}
calcAge() {
//calculate age using today's date and birthday
return this.calcAge();
}
bark() {
return console.log("Woof!");
}
updateAttendance() {
//add a day to the dog's attendance days at the petsitters
this._attendance++;
}
}
Methods often modify, update or delete data. Methods don’t have to update
data though. For example the bark() method doesn’t update any data because
barking doesn’t modify any of the attributes of the Dog class: name or birthday.
The updateAttendance() method adds a day the Dog attended the pet sitting camp.
The attendance attribute is important to keep track of for billing Owners at the
end of the month.
Inheritance: child classes inherit data and behaviors from parent class
Inheritance
If basic attributes and behaviors are defined in a parent class, child classes
can be created extending the functionality of the parent class, and adding
additional attributes and behaviors.
For example, herding dogs have the unique ability to herd animals. In other
words, all herding dogs are dogs, but not all dogs are herding dogs. We
represent this difference by creating a child class HerdingDog from the parent
class Dog, and then add the unique herd() behavior.
The benefits of inheritance are programs can create a generic parent class,
and then create more specific child classes as needed. This simplifies overall
programming, because instead of recreating the structure of the Dog class
multiple times, child classes automatically gain access to functionalities
within their parent class.
In the following code snippet, child class HerdingDog inherits the
method bark from the parent class Dog, and the child class adds an additional
method, herd().
class Dog{
_attendance = 0;
constructor(namee, birthday) {
this.name = name;
this.birthday = birthday;
getAge() {
//Getter
return this.calcAge();
calcAge() {
return this.calcAge();
bark() {
return console.log("Woof!");
}
updateAttendance() {
this._attendance++;
constructor(name, birthday) {
super(name);
super(birthday);
herd() {
Notice that the HerdingDog class does not have a copy of the bark() method, it
inherits the bark() method defined in the parent Dog class.
When the code calls fluffy.bark() method, the bark() method walks up the chain of
child to parent classes, to find where the bark method is defined.
class Dog{
constructor(namee, birthday) {
this.name = name;
this.birthday = birthday;
getAge() {
//Getter
return this.calcAge();
calcAge() {
return this.calcAge();
bark() {
return console.log("Woof!");
updateAttendance() {
this._attendance++;
}
//Child class HerdingDog, inherits from parent Dog
constructor(name, birthday) {
super(name);
super(birthday);
herd() {
fluffy.bark();
Note: Parent class is also known as super class, or base class. Child class can
also be called derived class, or extended class.
Rufus is an object instantiated from the parent class Dog, so Rufus only has
access to the bark() method.
Encapsulation
Let’s use a car as a metaphor for encapsulation. The information the car
shares with the outside world, using blinkers to indicate turns, are public
interfaces. In contrast, the engine is hidden under the hood.
It’s a private, internal interface. When you’re driving a car down the road,
other drivers require information to make decisions, like whether you’re
turning left or right. However, exposing internal, private data like the engine
temperature, would just confuse other drivers.
Encapsulation adds security. Attributes and methods can be set to private, so
they can’t be accessed outside the class. To get information about data in an
object, public methods & properties are used to access or update data.
This adds a layer of security, where the developer chooses what data can be
seen on an object by exposing that data through public methods in the class
definition.
Note: JavaScript has private and protected properties and methods. Protected
Fields are prefixed with a _ ; private fields are prefixed with a #. Protected fields
are inherited, private ones aren’t.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Parent class Dog
class Dog{
//Declare protected (private) fields
_attendance = 0;
constructor(namee, birthday) {
this.name = name;
this.birthday = birthday;
}
getAge() {
//Getter
return this.calcAge();
}
calcAge() {
//calculate age using today's date and birthday
return this.calcAge();
}
bark() {
return console.log("Woof!");
}
updateAttendance() {
//add a day to the dog's attendance days at the petsitters
this._attendance++;
}
}
//instantiate a new instance of Dog class, an individual dog named Rufus
Consider the getAge() method in our example code, the calculation details are
hidden inside the Dog class. The rufus object uses the getAge() method to
calculate Rufus’s age.
Encapsulating & updating data: Since methods can also update an object’s
data, the developer controls what values can be changed through public
methods.
Instead, developers create public methods that allow other developers to call
methods on an object. Ideally, these public methods come with documentation
for the external developers.
Adds security: Only public methods and attributes are accessible from
the outside
Abstraction
Abstraction means that the user interacts with only selected attributes and
methods of an object. Abstraction uses simplified, high level tools, to access a
complex object.
A driver only uses a small selection of tools: like gas pedal, brake, steering
wheel, blinker. The engineering is hidden from the driver. To make a car work,
a lot of pieces have to work under the hood, but exposing that information to
the driver would be a dangerous distraction.
Abstraction also serves an important security role. By only displaying selected
pieces of data, and only allowing data to be accessed through classes
and modified through methods, we protect the data from exposure. To
continue with the car example, you wouldn’t want an open gas tank while
driving a car.
Security
Polymorphism
Method Overriding
Method overriding could create a bark() method in the child class that overrides
the bark() method in the parent Dog class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Parent class Dog
class Dog{
//Declare protected (private) fields
_attendance = 0;
constructor(namee, birthday) {
this.name = name;
this.birthday = birthday;
}
getAge() {
//Getter
return this.calcAge();
}
calcAge() {
//calculate age using today's date and birthday
return this.calcAge();
}
bark() {
return console.log("Woof!");
}
updateAttendance() {
//add a day to the dog's attendance days at the petsitters
this._attendance++;
}
}
//Child class TrackingDog, inherits from parent
Method Overloading
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Parent class Dog
class Dog{
//Declare protected (private) fields
_attendance = 0;
constructor(namee, birthday) {
this.name = name;
this.birthday = birthday;
}
getAge() {
//Getter
return this.calcAge();
}
calcAge() {
//calculate age using today's date and birthday
return this.calcAge();
}
bark() {
return console.log("Woof!");
}
updateAttendance() {
//add a day to the dog's attendance days at the petsitters
this._attendance++;
}
updateAttendance(x) {
//adds multiple to the dog's attendance days at the petsitters
Method overriding
Method overloading
Conclusion
If you’d like to take a deep dive into OOP, Educative has courses for OOP in:
Java
JavaScript
Python
C++
C#