SlideShare a Scribd company logo
Object Oriented
Programming
Andi Nurkholis, S.Kom., M.Kom.
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2019-2020
March 16, 2020
5 Class & Object
2
OOP
Everything in OOP is associated with classes
and objects, along with its attributes and
methods.
3
Class
A Class is like an object constructor, or a "blueprint"
for creating objects.
A class describes the characteristics of objects in
general.
4
5
Create a Class
To create a class, use the keyword class:
public class MyClass {
int x = 5;
}
MyClass.java
Create a class named "MyClass" with a variable x:
Keyword class ClassName()
Create an Object
In Java, an object is created from a class. We have already created the
class named MyClass, so now we can use this to create objects.
To create an object of MyClass, specify the class name, followed by the
object name, and use the keyword new:
6
Class ObjectName = new Class();
Example
7
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
Create an object called "myObj" and print the value of x:
Multiple Objects
8
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj1 = new MyClass(); // Object 1
MyClass myObj2 = new MyClass(); // Object 2
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
}
Create two objects of MyClass:
Using Multiple Classes
You can also create an object of a class and access it in another class.
This is often used for better organization of classes (one class has all the
attributes and methods, while the other class holds the main() method
(code to be executed)).
Remember that the name of the java file should match the class name. In
this example, we have created two files in the same directory/folder:
• MyClass.java
• OtherClass.java
9
Example
10
class OtherClass {
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
public class MyClass {
int x = 5;
}
11
Java Class Attributes
In the previous slide, 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:
Create a class called "MyClass" with two attributes: x and y:
public class MyClass {
int x = 5, y = 3;
}
Accessing Attributes
12
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
You can access attributes by creating an object of the class, and by
using the dot syntax (.):
Modify Attributes
You can also modify attribute values:
13
public class MyClass {
int x;
public static void main(String[] args) {
MyClass myObj = new MyClass();
myObj.x = 40;
System.out.println(myObj.x);
}
}
Modify Attributes (cont.)
14
public class MyClass {
int x = 10;
public static void main(String[] args) {
MyClass myObj = new MyClass();
myObj.x = 25; // x is now 25
System.out.println(myObj.x);
}
}
Or override existing values:
15
Final Attributes
If you don't want the ability to override existing values, declare the
attribute as final:
The final keyword is useful when you want a variable to always store the
same value, like PI (3.14159...).
The final keyword is called a "modifier".
Example
16
public class MyClass {
final int x = 10;
public static void main(String[] args) {
MyClass myObj = new MyClass();
myObj.x = 25; // will generate an error:
cannot assign a value to a final variable
System.out.println(myObj.x);
}
}
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:
17
Example
18
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj1 = new MyClass(); // Object 1
MyClass myObj2 = new MyClass(); // Object 2
myObj2.x = 25;
System.out.println(myObj1.x); // Outputs 5
System.out.println(myObj2.x); // Outputs 25
}
}
19
Multiple Attributes
You can specify as many attributes as you want:
public class Person {
String fname = "John“, lname = "Doe"; int age = 24;
public static void main(String[] args) {
Person myObj = new Person();
System.out.println("Name: " + myObj.fname + " " +
myObj.lname);
System.out.println("Age: " + myObj.age);
}
}
Java Class Methods
20
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known
as functions.
Why use methods? To reuse code: define the code once, and use it
many times.
Example
Create a method named myMethod() in MyClass:
21
public class MyClass {
static void myMethod() {
System.out.println("Hello World!");
}
public static void main(String[] args) {
myMethod();
}
}
// Outputs "Hello World!"
Static vs. Non-Static
22
You will often see Java programs that have either static or public
attributes and methods.
In the example above, 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:
23
Example
public class MyClass {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called
without creating objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called
by creating objects");
}
Example (cont.)
24
// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would compile an error
MyClass myObj = new MyClass(); // Create an
object of MyClass
myObj.myPublicMethod(); // Call the public
method on the object
}
}
Access Methods With an Object
Create a Car object named myCar. Call the fullThrottle() and speed()
methods on the myCar object:
25
// Create a Car class
public class Car {
// Create a fullThrottle() method
public void fullThrottle() {
System.out.println("The car is going as fast
as it can!");
}
Example (cont.)
26
// Create a speed() method and add a parameter
public void speed(int maxSpeed) {
System.out.println("Max speed is: " +
maxSpeed);
}
27
Example (cont.)
// Inside main, call the methods on the myCar object
public static void main(String[] args) {
Car myCar = new Car(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method
}
}
// The car is going as fast as it can!
// Max speed is: 200
Using Multiple Classes
28
Like we specified in previous slide, it is a good practice to create an
object of a class and access it in another class.
Remember that the name of the java file should match the class name.
In this example, we have created two files in the same directory:
• Car.java
• OtherClass.java
Example
29
public class Car {
public void fullThrottle() {
System.out.println("The car is going as fast as
it can!");
}
public void speed(int maxSpeed) {
System.out.println("Max speed is: " + maxSpeed);
}
}
Example (cont.)
30
class OtherClass {
public static void main(String[] args) {
Car myCar = new Car(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method
}
}
Thank You, Next …
Relation between Classes
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2019-2020
Andi Nurkholis, S.Kom., M.Kom.
March 16, 2020

More Related Content

PDF
Java -lec-5
PPTX
Abstraction java
PDF
Methods in Java
PPTX
Java Method, Static Block
PPTX
java interface and packages
PPT
PPTX
Static Members-Java.pptx
PPT
Method overriding
Java -lec-5
Abstraction java
Methods in Java
Java Method, Static Block
java interface and packages
Static Members-Java.pptx
Method overriding

What's hot (20)

PPTX
Java Methods
PPT
Java Threads and Concurrency
PPS
Java Exception handling
PPTX
Java input
PPT
Packages in java
PPT
Java static keyword
PPT
Java Collections Framework
PPTX
Inheritance in java
PPTX
Inner classes in java
PPTX
Methods and constructors in java
PPTX
Interface in java
PDF
Java IO
PPTX
Java constructors
PPTX
Introduction to OOP concepts
PPT
Exception Handling
PPS
Wrapper class
PPTX
Inheritance in java
PDF
String handling(string class)
PPTX
Access modifiers in java
PDF
Java Serialization
Java Methods
Java Threads and Concurrency
Java Exception handling
Java input
Packages in java
Java static keyword
Java Collections Framework
Inheritance in java
Inner classes in java
Methods and constructors in java
Interface in java
Java IO
Java constructors
Introduction to OOP concepts
Exception Handling
Wrapper class
Inheritance in java
String handling(string class)
Access modifiers in java
Java Serialization
Ad

Similar to Object Oriented Programming - 5. Class & Object (20)

PPTX
Classes and Objects in C#
PDF
Intake 37 4
PPT
Java class
PPT
Object Oriented Programming Concept.Hello
PDF
C# (This keyword, Properties, Inheritance, Base Keyword)
PDF
OOPs & Inheritance Notes
DOCX
Introduction to object oriented programming concepts
PPTX
‫Chapter3 inheritance
PDF
Class and object in C++ By Pawan Thakur
PPTX
05 Object Oriented Concept Presentation.pptx
PPSX
Oop features java presentationshow
DOCX
OOP and C++Classes
PPT
Class methods
PPT
creating objects and Class methods
PDF
Object oriented programming
PPTX
Chapter4.pptxdgdhgfshsfhtgjsjryjusryjryjursyj
PPTX
11. Objects and Classes
PDF
The Ring programming language version 1.8 book - Part 80 of 202
PPTX
classes object fgfhdfgfdgfgfgfgfdoop.pptx
PDF
Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf
Classes and Objects in C#
Intake 37 4
Java class
Object Oriented Programming Concept.Hello
C# (This keyword, Properties, Inheritance, Base Keyword)
OOPs & Inheritance Notes
Introduction to object oriented programming concepts
‫Chapter3 inheritance
Class and object in C++ By Pawan Thakur
05 Object Oriented Concept Presentation.pptx
Oop features java presentationshow
OOP and C++Classes
Class methods
creating objects and Class methods
Object oriented programming
Chapter4.pptxdgdhgfshsfhtgjsjryjusryjryjursyj
11. Objects and Classes
The Ring programming language version 1.8 book - Part 80 of 202
classes object fgfhdfgfdgfgfgfgfdoop.pptx
Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf
Ad

More from AndiNurkholis1 (20)

PDF
Technopreneurship - 9 Analisis Biaya dan Keuangan
PDF
Pengantar Bisnis - 14 Manajemen Keuangan
PDF
Pengantar Bisnis - 13 Manajemen Operasi
PDF
Pengantar Bisnis - 12 Kebijakan Harga
PDF
Pengantar Bisnis - 11 Kebijakan Distribusi
PDF
Technopreneurship - 8 Manajemen Sumber Daya Manusia
PDF
Pengantar Bisnis - 10 Kebijakan Produk
PDF
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
PDF
Pengantar Bisnis - 9 Manajemen Pemasaran
PDF
Technopreneurship - 6 Business Plan
PDF
Pengantar Bisnis - 8 Kepemimpinan
PDF
Technopreneurship - 5 Model Bisnis
PDF
Technopreneurship - 4 Studi Kelayakan Usaha
PDF
Pengantar Bisnis - 7 Motivasi Kerja
PDF
Pengantar Bisnis - 6 Manajemen Sumber Daya Manusia
PDF
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
PDF
Technopreneurship - 3 Ide dan Prinsip Bisnis
PDF
Pengantar Bisnis - 4 Bentuk Organisasi Bisnis
PDF
Technopreneurship - 2 Pengantar Technopreneurship
PDF
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
Technopreneurship - 9 Analisis Biaya dan Keuangan
Pengantar Bisnis - 14 Manajemen Keuangan
Pengantar Bisnis - 13 Manajemen Operasi
Pengantar Bisnis - 12 Kebijakan Harga
Pengantar Bisnis - 11 Kebijakan Distribusi
Technopreneurship - 8 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 10 Kebijakan Produk
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
Pengantar Bisnis - 9 Manajemen Pemasaran
Technopreneurship - 6 Business Plan
Pengantar Bisnis - 8 Kepemimpinan
Technopreneurship - 5 Model Bisnis
Technopreneurship - 4 Studi Kelayakan Usaha
Pengantar Bisnis - 7 Motivasi Kerja
Pengantar Bisnis - 6 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
Technopreneurship - 3 Ide dan Prinsip Bisnis
Pengantar Bisnis - 4 Bentuk Organisasi Bisnis
Technopreneurship - 2 Pengantar Technopreneurship
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Advanced IT Governance
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
20250228 LYD VKU AI Blended-Learning.pptx
Modernizing your data center with Dell and AMD
Chapter 3 Spatial Domain Image Processing.pdf
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Weekly Chronicles - August'25 Week I
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
MYSQL Presentation for SQL database connectivity
Big Data Technologies - Introduction.pptx
NewMind AI Monthly Chronicles - July 2025
Transforming Manufacturing operations through Intelligent Integrations
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
GamePlan Trading System Review: Professional Trader's Honest Take
Understanding_Digital_Forensics_Presentation.pptx
Advanced IT Governance
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...

Object Oriented Programming - 5. Class & Object

  • 1. Object Oriented Programming Andi Nurkholis, S.Kom., M.Kom. Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 March 16, 2020
  • 2. 5 Class & Object 2
  • 3. OOP Everything in OOP is associated with classes and objects, along with its attributes and methods. 3
  • 4. Class A Class is like an object constructor, or a "blueprint" for creating objects. A class describes the characteristics of objects in general. 4
  • 5. 5 Create a Class To create a class, use the keyword class: public class MyClass { int x = 5; } MyClass.java Create a class named "MyClass" with a variable x: Keyword class ClassName()
  • 6. Create an Object In Java, an object is created from a class. We have already created the class named MyClass, so now we can use this to create objects. To create an object of MyClass, specify the class name, followed by the object name, and use the keyword new: 6 Class ObjectName = new Class();
  • 7. Example 7 public class MyClass { int x = 5; public static void main(String[] args) { MyClass myObj = new MyClass(); System.out.println(myObj.x); } } Create an object called "myObj" and print the value of x:
  • 8. Multiple Objects 8 public class MyClass { int x = 5; public static void main(String[] args) { MyClass myObj1 = new MyClass(); // Object 1 MyClass myObj2 = new MyClass(); // Object 2 System.out.println(myObj1.x); System.out.println(myObj2.x); } } Create two objects of MyClass:
  • 9. Using Multiple Classes You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)). Remember that the name of the java file should match the class name. In this example, we have created two files in the same directory/folder: • MyClass.java • OtherClass.java 9
  • 10. Example 10 class OtherClass { public static void main(String[] args) { MyClass myObj = new MyClass(); System.out.println(myObj.x); } } public class MyClass { int x = 5; }
  • 11. 11 Java Class Attributes In the previous slide, 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: Create a class called "MyClass" with two attributes: x and y: public class MyClass { int x = 5, y = 3; }
  • 12. Accessing Attributes 12 public class MyClass { int x = 5; public static void main(String[] args) { MyClass myObj = new MyClass(); System.out.println(myObj.x); } } You can access attributes by creating an object of the class, and by using the dot syntax (.):
  • 13. Modify Attributes You can also modify attribute values: 13 public class MyClass { int x; public static void main(String[] args) { MyClass myObj = new MyClass(); myObj.x = 40; System.out.println(myObj.x); } }
  • 14. Modify Attributes (cont.) 14 public class MyClass { int x = 10; public static void main(String[] args) { MyClass myObj = new MyClass(); myObj.x = 25; // x is now 25 System.out.println(myObj.x); } } Or override existing values:
  • 15. 15 Final Attributes If you don't want the ability to override existing values, declare the attribute as final: The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...). The final keyword is called a "modifier".
  • 16. Example 16 public class MyClass { final int x = 10; public static void main(String[] args) { MyClass myObj = new MyClass(); myObj.x = 25; // will generate an error: cannot assign a value to a final variable System.out.println(myObj.x); } }
  • 17. 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: 17
  • 18. Example 18 public class MyClass { int x = 5; public static void main(String[] args) { MyClass myObj1 = new MyClass(); // Object 1 MyClass myObj2 = new MyClass(); // Object 2 myObj2.x = 25; System.out.println(myObj1.x); // Outputs 5 System.out.println(myObj2.x); // Outputs 25 } }
  • 19. 19 Multiple Attributes You can specify as many attributes as you want: public class Person { String fname = "John“, lname = "Doe"; int age = 24; public static void main(String[] args) { Person myObj = new Person(); System.out.println("Name: " + myObj.fname + " " + myObj.lname); System.out.println("Age: " + myObj.age); } }
  • 20. Java Class Methods 20 A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions. Why use methods? To reuse code: define the code once, and use it many times.
  • 21. Example Create a method named myMethod() in MyClass: 21 public class MyClass { static void myMethod() { System.out.println("Hello World!"); } public static void main(String[] args) { myMethod(); } } // Outputs "Hello World!"
  • 22. Static vs. Non-Static 22 You will often see Java programs that have either static or public attributes and methods. In the example above, 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:
  • 23. 23 Example public class MyClass { // Static method static void myStaticMethod() { System.out.println("Static methods can be called without creating objects"); } // Public method public void myPublicMethod() { System.out.println("Public methods must be called by creating objects"); }
  • 24. Example (cont.) 24 // Main method public static void main(String[] args) { myStaticMethod(); // Call the static method // myPublicMethod(); This would compile an error MyClass myObj = new MyClass(); // Create an object of MyClass myObj.myPublicMethod(); // Call the public method on the object } }
  • 25. Access Methods With an Object Create a Car object named myCar. Call the fullThrottle() and speed() methods on the myCar object: 25 // Create a Car class public class Car { // Create a fullThrottle() method public void fullThrottle() { System.out.println("The car is going as fast as it can!"); }
  • 26. Example (cont.) 26 // Create a speed() method and add a parameter public void speed(int maxSpeed) { System.out.println("Max speed is: " + maxSpeed); }
  • 27. 27 Example (cont.) // Inside main, call the methods on the myCar object public static void main(String[] args) { Car myCar = new Car(); // Create a myCar object myCar.fullThrottle(); // Call the fullThrottle() method myCar.speed(200); // Call the speed() method } } // The car is going as fast as it can! // Max speed is: 200
  • 28. Using Multiple Classes 28 Like we specified in previous slide, it is a good practice to create an object of a class and access it in another class. Remember that the name of the java file should match the class name. In this example, we have created two files in the same directory: • Car.java • OtherClass.java
  • 29. Example 29 public class Car { public void fullThrottle() { System.out.println("The car is going as fast as it can!"); } public void speed(int maxSpeed) { System.out.println("Max speed is: " + maxSpeed); } }
  • 30. Example (cont.) 30 class OtherClass { public static void main(String[] args) { Car myCar = new Car(); // Create a myCar object myCar.fullThrottle(); // Call the fullThrottle() method myCar.speed(200); // Call the speed() method } }
  • 31. Thank You, Next … Relation between Classes Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 Andi Nurkholis, S.Kom., M.Kom. March 16, 2020