Object Oriented Programming Lecture 1
Object Oriented Programming Lecture 1
ORIENTED
PROGRAMMING
Introduction to Object
Oriented Programming
AGENDA
Java OOP
Classes and Objects
Attributes
Methods
Constructors
JAVA – WHAT IS OOP? 3
PROCEDURAL
PROGRAMMING
• Procedural programming is about writing
procedures or methods that perform
operations on the data.
Presentation title 5
Presentation title 6
OBJECT ORIENTED
PROGRAMMING
• Object oriented programming is about
creating objects that contain both data and
methods.
7
The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code.
You should extract out the codes that are common for the application, and place them
at a single place and reuse them instead of repeating it.
Presentation title 9
CLASSES AND
OBJECTS
CLASSES AND OBJECTS
• Classes and objects are the two main aspects of object-oriented programming.
• Look at the following illustration to see the difference between class and objects:
ANOTHER EXAMPLE
Vehicle
Automobile
• Everything in Java is associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car has attributes, such as
weight and color, and methods, such as drive and brake.
• In Java, an object is created from a class. To create an object of Main, (1) specify the class name, (2) followed
by object name, and use keyword new:
Presentation title 14
MULTIPLE OBJECTS
MULTIPLE
CLASSES
Create 2 folders : You can also create an object of a class and
• Main.java access it in another class.
• Second.java
MULTIPLE CLASSES
When both files are compiled, run the Second.java file to get the
output
Presentation title 17
JAVA CLASS
ATTRIBUTES
JAVA CLASS ATTRIBUTES
In the previous chapter, we used the term "variable" for x in the example (as shown below).
It is actually an attribute of the class. Or you could say that class attributes are variables within a class:
Presentation title 19
ACCESSING ATTRIBUTES
You can access attributes by creating an object of the class, and by using the dot syntax (.):
The following example will create an object of the Main class, with the name myObj. We use the x attribute on the object to print its value:
OVERRIDE AND MODIFY
Presentation title 20
ATTRIBUTES
• With Java attribute, you can also modify the value of the class attribute:
• You can also override the value as well. Try assigning the values in your Main class where x = 5, and in your
method where myObj.x = 25.
Presentation title 21
MULTIPLE OBJECTS
• If you create multiple objects of one class, you can change the attribute values in one
object, without affecting the attribute values in the other.
MULTIPLE ATTRIBUTES
• You can also specify multiple attributes as you want:
Presentation title 23
JAVA CLASS
METHODS
myMethod() prints a text (the
action), when it is called. To call a
method, write the method's name
followed by two parentheses () and
a semicolon;
STATIC VS PUBLIC
Presentation title 25
You will often see Java programs that have either static or public attributes and methods.
In the previous example, we created a static method, which means that it can be accessed without creating
an object of the class, unlike public, which can only be accessed by objects:
Presentation title
ACCESS METHODS WITH AN 26
OBJECT
• Like specified before, it is a good practice to create an object of a class and access it in another class.
• In this example, create 2 files in the same directory just like before as shown in the next slide:
Presentation title 28
Main.java
Second.java
JAVA
CONSTRUCTORS
CONSTRUCTORS
• A constructor in Java is a special method that is used to initialize objects. The
constructor is called when an object of a class is created. It can be used to set initial
values for object attributes:
Presentation title 31
CONSTRUCTOR PARAMETERS
Constructors can also take parameters, which is used to initialize attributes.
The following example adds an int y parameter to the constructor. Inside the
constructor we set x to y (x=y).
When we call the constructor, we pass a parameter to the constructor (5), which will
set the value of x to 5:
THANK YOU
See you guys next week.