Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

Win Java Training Program

Java Class and Object

By
Madhumeeta Bhatia
Contents

 Class and An Object


 Method
 Constructor
 Garbage collection

www.wincorporatetraining.com 2
Java Class
 Template which contains variables, methods,
constructors and properties.
 Syntax
class <classname> {
// variable
// method
// constructor
}
 Variable – Object Property
 Methods – Object Functionality

www.wincorporatetraining.com 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.

www.wincorporatetraining.com 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.

www.wincorporatetraining.com 5
Method

 Called as function in C and C++.


 Contains two parts
 Method Declaration
void showInfo();
 Method Definition
void showInfo() {
// java statements
}

www.wincorporatetraining.com 6
Method

 Purpose
 To Assign value
 To Perform calculation
 To Print the Output.
 Takes Parameters
void assign(int empno, String eno) {
……
……
}

www.wincorporatetraining.com 7
Method

 Method returns a Value


int Sum() {
return a+b;
}
 Called by using Object of the class.

www.wincorporatetraining.com 8
Class Diagram

Employee

Empno:int
Ename:String

Assign():void
showInfo():void

www.wincorporatetraining.com 9

You might also like