Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

2-Java Classes With Objects and Attributes

This document discusses object-oriented programming concepts in Java including classes, objects, attributes, and how to define and access them. It provides examples of creating classes with attributes, instantiating objects from classes, accessing and modifying attribute values of objects, creating multiple objects from the same class and modifying attributes of individual objects without affecting others.

Uploaded by

Minosh Perera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

2-Java Classes With Objects and Attributes

This document discusses object-oriented programming concepts in Java including classes, objects, attributes, and how to define and access them. It provides examples of creating classes with attributes, instantiating objects from classes, accessing and modifying attribute values of objects, creating multiple objects from the same class and modifying attributes of individual objects without affecting others.

Uploaded by

Minosh Perera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Object Oriented System Design - ICT 325 (3)

Java Classes with Objects and Attributes

Dr. R. A. H. M. Rupasingha
Senior Lecturer in Computer Science
Content
• Java Classes/Objects
• Multiple Objects
• Multiple Classes
• Java Classes/Attributes
• Accessing Attributes
• Modify Attributes
• Multiple Objects
• Multiple Attributes 2
Java - What is OOP?

• OOP stands for Object-Oriented Programming.


• Procedural programming is about writing procedures or methods
that perform operations on the data, while object-oriented
programming is about creating objects that contain both data
and methods.

3
Java Classes/Objects
• Classes and objects are the two main aspects of object-oriented
programming.
• Eg:

• So, a class is a template for objects, and an object is an instance of a


class.
• When the individual objects are created, they inherit all the variables
and methods from the class.
4
Java Classes/Objects - Example 01
1. Create a Class
• To create a class, use the keyword class.

Eg:
• Create a class named "MyClass" with a variable x:

MyClass.java
public class MyClass {
int x = 5;
}
5
Java Classes/Objects - Example 01 (Cont.)
2. 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:

Eg (Cont.):
Create an object called "myObj" and print the value of x:
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass(); Output:
System.out.println(myObj.x); 5
}
} 6
Content
• Java Classes/Objects
• Multiple Objects
• Multiple Classes
• Java Classes/Attributes
• Accessing Attributes
• Modify Attributes
• Multiple Objects
• Multiple Attributes 7
Multiple Objects - Example 02
• You can create multiple objects of one class:
Eg:
Create two objects of MyClass:
public class MyClass {
int x = 5;

public static void main(String[] args) {


MyClass myObj1 = new MyClass(); // Object 1
Output:
MyClass myObj2 = new MyClass(); // Object 2
System.out.println(myObj1.x); 5
System.out.println(myObj2.x); 5
}
} 8
Content
• Java Classes/Objects
• Multiple Objects
• Multiple Classes
• Java Classes/Attributes
• Accessing Attributes
• Modify Attributes
• Multiple Objects
• Multiple Attributes 9
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)).

Eg:
• Let’s create two files in the same directory/folder:
• MyClass.java
• OtherClass.java

10
Multiple Classes – Example 03
MyClass.java
public class MyClass {
int x = 5;
}

Output:
OtherClass.java 5
class OtherClass {
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
} 11
Content
• Java Classes/Objects
• Multiple Objects
• Multiple Classes
• Java Classes/Attributes
• Accessing Attributes
• Modify Attributes
• Multiple Objects
• Multiple Attributes 12
Java Class Attributes
• In the previous part, 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.

Eg:
• Create a class called "MyClass" with two attributes: x and y:
pclass MyClass {
int x = 5;
int y = 3;
}
13
Content
• Java Classes/Objects
• Multiple Objects
• Multiple Classes
• Java Classes/Attributes
• Accessing Attributes
• Modify Attributes
• Multiple Objects
• Multiple Attributes 14
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 MyClass class,


with the name myObj. We use the x attribute on the object to
print its value:
public class MyClass {
int x = 5;
Check again previous public static void main(String[] args) {
Example 01: MyClass myObj = new MyClass(); Output:
System.out.println(myObj.x); 5
}
} 15
Content
• Java Classes/Objects
• Multiple Objects
• Multiple Classes
• Java Classes/Attributes
• Accessing Attributes
• Modify Attributes
• Multiple Objects
• Multiple Attributes 16
Modify Attributes – Example 04

• You can also modify attribute values:


• Set the value of x to 40:
public class MyClass {
int x;

public static void main(String[] args) {


Output:
MyClass myObj = new MyClass();
myObj.x = 40; 40
System.out.println(myObj.x);
}
}
17
Modify Attributes – Example 05

• Or override existing values:


• Change the value of x to 25:

public class MyClass {


int x = 10;

public static void main(String[] args) { Output:


MyClass myObj = new MyClass(); 25
myObj.x = 25; // x is now 25
System.out.println(myObj.x);
}
}
18
Modify Attributes – Example 06
• 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.
• The final keyword is called a "modifier". (See Lec 11 in last semester)
public class MyClass {
final int x = 10;
Output:
Exception in thread "main"
public static void main(String[] args) { java.lang.Error: Unresolved
MyClass myObj = new MyClass(); compilation problem: The final field
MyClass.x cannot be assigned
myObj.x = 25; // will generate an error: cannot assign a value to a final variable at
System.out.println(myObj.x); MyClass.main(MyClass.java:6)

}
} 19
Content
• Java Classes/Objects
• Multiple Objects
• Multiple Classes
• Java Classes/Attributes
• Accessing Attributes
• Modify Attributes
• Multiple Objects
• Multiple Attributes 20
Multiple Objects – Example 07
• 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:
• Change the value of x to 25 in myObj2, and leave x in myObj1
unchanged:
public class MyClass {
int x = 5;

public static void main(String[] args) {


MyClass myObj1 = new MyClass(); // Object 1 Output:
MyClass myObj2 = new MyClass(); // Object 2 5
myObj2.x = 25; 25
System.out.println(myObj1.x); // Outputs 5
System.out.println(myObj2.x); // Outputs 25
}
} 21
Content
• Java Classes/Objects
• Multiple Objects
• Multiple Classes
• Java Classes/Attributes
• Accessing Attributes
• Modify Attributes
• Multiple Objects
• Multiple Attributes 22
Multiple Attributes – Example 08
• You can specify as many attributes as you want:

public class MyClass {


String fname = "Nimali";
String lname = "Jayarathne";
int age = 24;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println("Name: " + myObj.fname + " " + myObj.lname);
System.out.println("Age: " + myObj.age);
} Output:
Name: Nimali Jayarathne
} 23
Age: 24
Summary
• Java Classes/Objects
• Multiple Objects
• Multiple Classes
• Java Classes/Attributes
• Accessing Attributes
• Modify Attributes
• Multiple Objects
• Multiple Attributes 24
Questions?

End. 25

You might also like