OOP Basics
OOP Basics
[email protected]
Class: FRUITS
Objects
[email protected]
Class: Phones
Objects
[email protected]
Class is Blue print/template for Objects
Class is collection of similar kind of Objects
Objects are instance/copy/examples of Class
Objects Have
Features/attributes Functions/Methods
[email protected]
MOBILE PHONE
Features Functions
1)Color 1)Calling
2)Brand 2)Clicking Pictures
3)Camera Quality 3)Video
4)Battery Life 4) Scanning document
5)Memory 5)Internet
6)Model Number 6)Messaging
Features Functions
1)Color 1)Speed
2)Brand 2)Move
3)Fuel 3)Break
4)Build Year
5)Type
6)Model
7)Number
[email protected]
Student
Features Functions
1)Roll No 1)Print Name
2)Name 2)Results
3)Last Name
4)Class
5)Section
6)Marks Obtained
[email protected]
Student class
Functions
1)Print Name()
2)Results()
Student class
Student1.printName();
Functions
Student1.results(); 1)Print Name()
2)Results()
[email protected]
Object-oriented programming: As the name suggests, Object-Oriented Programming or
OOPs refers to languages that uses objects in programming. Object-oriented
programming aims to implement real-world entities like inheritance, hiding,
polymorphism etc in programming. The main aim of OOP is to bind together the data
and the functions that operate on them so that no other part of the code can access this
data except that function.
[email protected]
Steps to create Class in P5 Editor
[email protected]
Add New File
[email protected]
Class
Name Click Here
To File
[email protected]
Click to open file
Click to open file
[email protected]
Keyword:Class
Class Name
Class names should be nouns, in mixed case with the first letter of each internal word
capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid
acronyms and abbreviations. class Watch;class Car;
[email protected]
Keywords are tokens that have special
meaning in JavaScript
•
if, • break,
• in, • case,
• instanceof,
• catch,
• continue,
• new,
• debugger,
• return,
• default,
• switch, • delete,
• this, • do,
• throw, • else,
• try, • finally,
• typeof, • for,
• var, • function,
• void, • class,
• const,
• while,
• enum,
• with
[email protected]
Class Structure
class MyClass {
// class methods constructor() { ...}
method1() { ... }
method2() { ... }
method3() { ... }
... }
x
Diameter
Variable/fields/features
Function/Method
[email protected]
A constructor is a function that creates an
instance of a class which is typically called
an “object”. In JavaScript,
a constructor gets called when you
declare an object using the new keyword.
The purpose of a constructor is to create
an object and set values if there are any
object properties present.
A constructor is a function that creates an instance/copy of a
class which is typically called an “object”
. In JavaScript, a constructor gets called when
you declare an object using the new keyword.
The purpose of a constructor is to create an object and
set values if there are any object properties present.
The this keyword refers to the current object in a method or constructor.
[email protected]
Class:Car
We want Object
Object this car
Object
[email protected]
• If we have multiple copy of a car then which
car I am currently talking about we refer it as
this..
• In the same way this here refer to current
objects variable/methods.
[email protected]
Step -2 Add Class file in Index.html
[email protected]
Add in Script Tag
[email protected]
Creating a memory location and naming it as
tennisBall
[email protected]
Keyword var
Variables are the names you give to computer memory locations which are used to
store values in a computer program.
For example, assume you want to store two values 10 and 20 in your program and
at a later stage, you want to use these two values. Let's see how you will do it. Here
are the following three simple steps −
It creates a new object. The type of this object is simply object.(Ball here).Creating a copy
or instance or example of Ball and Assigned to memory [email protected]
tennisBall
Use of new Keyword
• It creates a new object. The type of this object is
simply object.
• It makes the this variable point to the newly
created object.
• It executes the constructor function, using the
newly created object whenever this is
mentioned.
• It returns the newly created object.
[email protected]
Calling Function
[email protected]
[email protected]
Creating two objects/instances of class Ball.
Changing
default
value of
variable
[email protected]
Three Objects
[email protected]
Adding color feature to Ball Class
[email protected]
Done
[email protected]
Object-oriented programming: As the name suggests, Object-Oriented Programming or
OOPs refers to languages that uses objects in programming. Object-oriented
programming aims to implement real-world entities like inheritance, hiding,
polymorphism etc in programming. The main aim of OOP is to bind together the data
and the functions that operate on them so that no other part of the code can access
this data except that function.
A class is a blueprint that defines the variables and the methods common to all objects
of a certain kind.