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

Lecture 1-Class Objects

Uploaded by

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

Lecture 1-Class Objects

Uploaded by

tajrianaltahrim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Lecture 1

Kazi Rifat Ahmed


Lecturer
Department of Software Engineering
Daffodil International University
Class & Objects
What is Object
➔ An object in Java is the physical as well as a logical entity, whereas, a class
in Java is a logical entity only.
➔ An entity that has state and behavior is known as an object e.g., chair,
bike, marker, pen, table, car, etc.
➔ It can be physical or logical (tangible and intangible). The example of an
intangible object is the banking system.
What is Object (Cont.)
What is Object (Cont.)
➔ An object has three characteristics:
◆ State: represents the data (value) of an object.
◆ Behavior: represents the behavior (functionality) of an object such
as deposit, withdraw, etc.
◆ Identity: An object identity is typically implemented via a unique
ID. The value of the ID is not visible to the external user. However, it
is used internally by the JVM (Java Virtual Machine) to identify each
object uniquely.
For Example, Pen is an object. Its name is Reynolds; color is white, known
as its state. It is used to write, so writing is its behavior.
What is Object (Cont.)
An object is an instance of a class. 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.
What is Class
A class is a group of objects which have common properties.
It is a template or blueprint from which objects are created. It is a logical entity.
It can't be physical.
A class in Java can contain:
● Fields
● Methods
● Constructors
● Blocks
● Nested class and interface
What is Class (Cont.)
What is Class (Cont.)
Syntax to declare a class:

class <class_name>{
field;
method;
}
Instance Variable in Java
➢ A variable which is created inside the class but outside the method is
known as an instance variable.
➢ Instance variable doesn't get memory at compile time.
➢ It gets memory at runtime when an object or instance is created.
➢ That is why it is known as an instance variable.
Method in Java
➢ In Java, a method is like a function which is used to expose the behavior of
an object.

➢ Advantage of Method
○ Code Reusability
○ Code Optimization
New Keyword in Java
➔ The new keyword is used to allocate memory at runtime.
➔ All objects get memory in Heap memory area.
Constructor in Java
● In Java, a constructor is a block of codes similar to the method.
● It is called when an instance of the class is created.
● At the time of calling constructor, memory for the object is allocated in the
memory.
Object and Class Example: main within
the class
In this example, we have created a Student class which has two data members
id and name. We are creating the object of the Student class by new keyword
and printing the object's value.
Here, we are creating a main() method inside the class.
File: Student.java
Object and Class Example: main within
the class
//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance variable Output:
String name; 0
//creating main method inside the Student class Null
null
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name); }
}
Object and Class Example: main outside
the class
In real time development, we create classes and use it from another class. It is
a better approach than previous one. Let's see a simple example, where we are
having main() method in another class.
We can have multiple classes in different Java files or single Java file. If you
define multiple classes in a single Java source file, it is a good idea to save the
file name with the class name which has main() method.
File: TestStudent1.java
Object and Class Example: main outside
the class
//Java Program to demonstrate having the main method in
//another class
//Creating Student class.
class Student{
Output:
int id;
0
String name;
null
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name); }
}
3 Ways to Initialize Object
There are 3 ways to initialize object in Java.

1. By reference variable
2. By method
3. By constructor
1) Object and Class Example: Initialization through
reference
Initializing an object means storing data into the object. Let's see a simple example
where we are going to initialize the object through a reference variable.
File: TestStudent2.java
class Student{
int id;
String name;
}
class TestStudent2{ Output:
public static void main(String args[]){
Student s1=new Student(); 101 Sonoo
s1.id=101;
s1.name="Sonoo";
System.out.println(s1.id+" "+s1.name);//printing members with a white space
}
}
1) Object and Class Example: Initialization through
reference (Cont.)
We can also create multiple objects and store information in it through reference variable.
File: TestStudent3.java

class Student{ //Initializing objects


int id; s1.id=101;
String name; s1.name="Sonoo";
} s2.id=102; Output:
class TestStudent3{ s2.name="Amit"; 101 Sonoo
public static void main(String args[]){ //Printing data 102 Amit
//Creating objects System.out.println(s1.id+" "+s1.name);
Student s1=new Student(); System.out.println(s2.id+" "+s2.name);
Student s2=new Student(); }
}
2) Object and Class Example: Initialization
through method
In this example, we are creating the two objects of Student class and initializing the value to these objects
by invoking the insertRecord method. Here, we are displaying the state (data) of the objects by invoking
the displayInformation() method.
File: TestStudent4.java

class Student{
int rollno; class TestStudent4{
String name; public static void main(String args[]){ Output:
void insertRecord(int r, String n){ Student s1=new Student();
111 Karan
rollno=r; Student s2=new Student();
name=n; s1.insertRecord(111,"Karan"); 222 Aryan
} s2.insertRecord(222,"Aryan");
void displayInformation() s1.displayInformation();
{System.out.println(rollno+" "+name);} s2.displayInformation();
} }
}
3) Object and Class Example: Initialization through a
constructor
We will learn about constructors in Java later.
Object and Class Example: Employee
Let's see an example where we are maintaining records of employees.
File: TestEmployee.java
class Employee{ public class TestEmployee {
int id; public static void main(String[] args) {
String name; Employee e1=new Employee(); Output:
float salary; Employee e2=new Employee(); 101 ajeet 45000.0
void insert(int i, String n, float s) { Employee e3=new Employee();
id=i; e1.insert(101,"ajeet",45000); 102 irfan 25000.0
name=n; e2.insert(102,"irfan",25000);
103 nakul 55000.0
salary=s; e3.insert(103,"nakul",55000);
} e1.display();
void display(){System.out.println(id+" e2.display();
"+name+" "+salary);} e3.display(); }
} }
References
1. https://www.javatpoint.com/object-and-class-in-java
2. https://en.wikipedia.org/wiki/Java_virtual_machine#:~:text=A%20Java%20virtual
%20machine%20
(JVM,required%20in%20a%20JVM%20implementation.
3. https://www.javatpoint.com/java-constructor
Thank You

You might also like