KEMBAR78
Object Oriented Programming - 5. Class & Object | PDF
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

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 OOPis associated with classes and objects, along with its attributes and methods. 3
  • 4.
    Class A Class islike an object constructor, or a "blueprint" for creating objects. A class describes the characteristics of objects in general. 4
  • 5.
    5 Create a Class Tocreate 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 InJava, 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 classMyClass { 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 Youcan 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 { publicstatic void main(String[] args) { MyClass myObj = new MyClass(); System.out.println(myObj.x); } } public class MyClass { int x = 5; }
  • 11.
    11 Java Class Attributes Inthe 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 classMyClass { 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 canalso 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 publicclass 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 youdon'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 youcreate 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 canspecify 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 Amethod 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 methodnamed 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 Youwill 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 // Mainmethod 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 Withan 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 // Createa speed() method and add a parameter public void speed(int maxSpeed) { System.out.println("Max speed is: " + maxSpeed); }
  • 27.
    27 Example (cont.) // Insidemain, 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 Likewe 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