4. Object
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.
5. 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 to identify each object uniquely.
6. Cont…
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.
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.
7. 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
8. A class contains variable declarations and methoddefinitions
Methods
definitio
ns
Variable
declaratio
ns
Method definitions
Variable declarations
(variable describes the
attributes)
Variable may be: instance variables or static variablesor final
variables
(methods handle the behavior)
Methods may be: instance methods or static methods
9. Defining a Class in java
Define an Employee class with instance variables and instance methods
class Employee
Instance methods
• setId(…)
• setName(…)
• setSalary(…)
•
getEmployeeDetails()
class Employee{
Instance variables
• id
• name
•salary
int id;
String name;
int salary;
void setId(int i) {
id = i;
}
void setName(String n) {
name = n;
}
void setSalary(int s) {
salary = s;
}
void getEmployeeDetails( ) {
System.out.println (name + “ salary
is “ + salary);
}
}
10. public class Account
{
double balance;
public void deposit( double amount)
{ balance += amount;}
public double withdraw( double amount ){
int minimum_balance=5000;
if (balance >= (amount+minimum_balance))
{
balance -= amount;
return amount;
Instance
Variable
Parameter
or argument
local
Variable
12. The previous slide contains definition of a class calledAccounts.
A class contains members which can either be variables(fields) ormethods(behaviors).
A variable declared within a class(outside any method) is known as an instancevariable.
A variable declared within a method is known as localvariable.
Variables with method declarations are known as parameters or arguments.
A class variable can also be declared as static where as a local variable cannot bestatic.
Member variables
13. Objects and References
Once a class is defined, you can declare a variable (object reference) of typeclass
Student stud1;
Employee emp1;
The new operator is used to create an object of that reference type
Employee emp = new Employee();
Object references are used to store objects.
Reference can be created for any type of classes (like concrete classes, abstract classes)
and interfaces.
object
Object reference
14. Objects and References (Contd.).
The new operator,
Dynamically allocates memory for an object
Creates the object on the heap
Returns a reference to it
The reference is then stored in the variable
15. Object and Class Example: main within the class
class Student
{
int id;//field or data member or instance variable
String name;
public static void main(String args[])
{
Student s1=new Student(); //creating an object of Student
s1.id=1001;
s1.name="John";
System.out.println(s1.id); //accessing member through reference variable
System.out.println(s1.name);
}
}
OUTPUT
1001
John
16. Object and Class Example: main outside the class
class Student
{
int id;
String name;
}
//Creating another class SampleStudent1 which contains the main method
class SampleStudent1
{
public static void main(String args[])
{
Student s1=new Student();
s1.id=1001;s1.name="John ";
System.out.println(s1.id);
System.out.println(s1.name);
} }
OUTPUT
1001
John
17. Object and Class Example: Initialization through method
class Student
{
int rollno;
String name;
void insertRecord(int r, String n)
{
rollno=r;
name=n;
}
void displayInformation()
{
System.out.println(rollno+" "+name);
}
}
class SampleStudent
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(1001,"Arun");
s2.insertRecord(1002,"Abi");
s1.displayInformation();
s2.displayInformation();
}
}
OUTPUT
1001 Arun
1002 Abi
18. Example
import java.util.*;
class Rectangle
{
int length;
int width;
void insert(int l, int w)
{
length=l; width=w;
}
int calculateArea()
{
return(length*width);
}
}
public class HelloWorld
{
public static void main(String []args)
{
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
Scanner S=new Scanner(System.in);
int l=S.nextInt();
int b=S.nextInt();
int l1=S.nextInt();
int b1=S.nextInt();
r1.insert(l,b);
r2.insert(l1,b1);
System.out.println("Area of the rectangle1="+r1.calculateArea()+" Sq.units");
System.out.println("Area of the rectangle2="+r2.calculateArea()+" Sq.units");
}
}
OUTPUT
2
5
12
5
Area of the rectangle1=10 Sq.units
Area of the rectangle2=60 Sq.units
19. public class EmployeeDemo
{
public static void main(String[] args)
{
Employee emp1 = new Employee();
emp1.setId(101);
emp1.setName("John");
emp1.setSalary(12000);
emp1.getEmployeeDetail();
}
}
class Employee
{
int id;
String name;
int salary;
void setId(int no)
{
id = no;
}
void setName(String n)
{ name = n;}
void setSalary(int s)
{ salary = s;}
void getEmployeeDetails()
{
System.out.println(name + " salary is "+ salary);
}
}
Output:
John salary is 12000
Example