Win Java Training Program
Java Class and Object
By
Madhumeeta Bhatia
Contents
Class and An Object
Method
Constructor
Garbage collection
[Link] 2
Java Class
Template which contains variables, methods,
constructors and properties.
Syntax
class <classname> {
// variable
// method
// constructor
}
Variable – Object Property
Methods – Object Functionality
[Link] 3
Java Class
Write a program to define a class “Emp” with the
variable empno and ename.
Define a Method called assign – to store the value
in the variable.
Define a Method showInfo() – to print the
employee details.
[Link] 4
Object
Instance of a Class.
Used to access the properties and methods of the
class.
Syntax [To Create Object]
<class name> <object Name> = new <constructor
name>();
Example
Employee objE = new Employee();
Note: new operator dynamically allocates
memory for the object.
[Link] 5
Method
Called as function in C and C++.
Contains two parts
Method Declaration
void showInfo();
Method Definition
void showInfo() {
// java statements
}
[Link] 6
Method
Purpose
To Assign value
To Perform calculation
To Print the Output.
Takes Parameters
void assign(int empno, String eno) {
……
……
}
[Link] 7
Method
Method returns a Value
int Sum() {
return a+b;
}
Called by using Object of the class.
[Link] 8
Class Diagram
Employee
Empno:int
Ename:String
Assign():void
showInfo():void
[Link] 9