Lecture - 4-Class and Object
Lecture - 4-Class and Object
Darwin Vargas
Prof
Contents
• Class and Object in Java
2
Class and Objects
3
Classes and 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.
⮚ Data member/variables
⮚Method
⮚Constructor
⮚Block of Statements
⮚Class or Interface
5
*** Syntax of Creating a Class
***Class Names - For all class names the first letter should be in Upper Case.
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.
7
***Syntax of Creating an Object
***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:
int height;
int width;
box1.height = 10;
box1.width = 20;
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:
}
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