0% found this document useful (0 votes)
5 views

Lecture_4-Class and Object

The lecture covers the fundamental concepts of classes and objects in Java, emphasizing their role in Object Oriented Programming. It explains the definition and syntax for creating classes and objects, along with examples demonstrating their usage. Additionally, it introduces UML notations for class representation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture_4-Class and Object

The lecture covers the fundamental concepts of classes and objects in Java, emphasizing their role in Object Oriented Programming. It explains the definition and syntax for creating classes and objects, along with examples demonstrating their usage. Additionally, it introduces UML notations for class representation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture – 4

Class and Object in JAVA

Shahriar Hossain Shanto


Lecturer
Department of CSE
Daffodil International University
Contents
• Class and Object in Java

• UML notations for class

2
Class and Objects

3
Classes and Objects

• Classes and Objects are basic concepts of Object Oriented Programming


which revolve around the real life entities.

• Everything in OOP is associated with classes and objects.

• A Class is like an object constructor, or a "blueprint" for creating objects.

4
1. Class
• A class is a user defined blueprint or prototype from which objects are created.

• It represents the set of properties or methods that are common to all objects of one
type.

• A java class can contains:

⮚ Data member/variables
⮚ Method

⮚ Constructor

⮚ Block of Statements

⮚ Class or Interface
5
*** Syntax of Creating a Class

modifiers class ClassName{


//body of the class
}

***Class Names - For all class names the first letter should be in Upper Case.

Example: public class Box { }

public class MyBox { }

6
2. Object
• A class is a template or blueprint from which objects are created. So, an object is the
instance(result) of a class.

• Object Definitions:
⮚ An object is a real-world entity.

⮚ An object is a runtime entity.

⮚ The object is an entity which has state and behavior.

⮚ The object is an instance of a class.

7
***Syntax of Creating an Object

ClassName objectName = new default_Constructor();

***Object Names - For all object names the first letter should be in lower Case.

8
Example - 1

• Create a Box Class. Each box of this class will have height and width.

• Create one object of the Box Class and display the information.

9
Solution of Example - 1:

public class Box {

int height;
int width;

public static void main(String[] args) {

Box box1 = new Box();

box1.height = 10;
box1.width = 20;

System.out.println("\nHeight of box1 = " + box1.height);


System.out.println("Width of box1 = " + box1.width);

10
Example - 2

• Create a Person Class. Each person of this class will have name and age.

• Create two objects of the Person Class, Set the values and display the
information.

11
UML: Unified Modeling Language
UML Representation of CLASS

ClassName

Instance Variables

Methods
UML representation of Class from Example - 1

Box
height: int
width: int

+ main(String[]) : void

13
Example – 3: Write a Java code from this UML

Student

- name: String
- id: int

+ main(String[]) : void

• Create two objects of the Student Class, Set the values and display the information.
14
Solution of Example - 3:

public class Student {

Student std2 = new Student();


private String name;
std2.name = "Kabir";
private int id;
std2.id = 200;
private double cgpa;
std2.cgpa = 3.8;

public static void main(String[] args)


System.out.println("\n\nName of Student -1 : "+std2.name);
{
System.out.println("ID of Student - 1 : "+std2.id);
Student std1 = new Student();
System.out.println("CGPA of Student - 1: "+std2.cgpa);
std1.name = "Abdullah";
}
std1.id = 100;
std1.cgpa = 3.5;

}
System.out.println("Name of Student -1 : "+std1.name);
System.out.println("ID of Student - 1 : "+std1.id);
System.out.println("CGPA of Student - 1: "+std1.cgpa); 15
Example – 4: Write a Java code from this UML

Department

- deptName: String
- deptCode: int
- faculty: String

+ main(String[]) : void

• Create two objects of the Department Class, Set the values and display the information.
16
Thank you!

17

You might also like