We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 77
_—
In this module you will learn - mp
TEKNOTURF
* Classes and Objects
* Access Specifiers
* Usage of Getters and SettersClasses and Objects
What is an Object
* Any real world entity with well defined properties is called an
object.
* Object will have a state, behaviour and a unique identity
* Objects can be tangible (physical entity) or intangible (cannot be
touched or felt)
Awooden chair Achemical process A transactionClasses and Objects ap
Wel ics
* Aclass is a template or blue print that describes
the behaviors/states that an object of its type
supports
* Aclass defines its
- object’s properties and
- object’s behavior through methodsTEKNOTURF
Classes and Objects
ere eed ag secre)
rT Tee eg Lee Lael a
Re es tee eee ceric
* Class is a description of the object
* It describes the data that each object possesses
* It also describes the various behaviors of the object
Oy ead eS Ute RrClasses and Objects
Employee 1D :101
Name : Rohith
Salary : 25000.00
ia
Class: Employee Employee: Object
=
=
TEKNOTURF
class Employee
{
//Attributes
int emplD;
String name;
double salary;
{Methods
void punchCard()
{
/1d0 something
}
void doProject()
{
Ido something
}
}Classes and Objects
TEKNOTURF
model = “Mustang”
Color = “Yellow”
numofPassengers = 0
amtOfGas = 16.5,
Behaviour:
‘Add / Remove Passenger
Get Tank Filled
Report when out of Gas
class Car
{
//mtributes
String model;
String color;
int numOfPassengers;
floatamtOfGas;
{Methods
void addPassenger()
{
// do something
}
void removePassenger()
; 1 do something
velftnip
; // do something
voldreprt wots)
: 1 do something
; }Class Declaration
Declaring a class
eC eomiCont
Sea eR ena”
Reeietetat ei
Example:
Cite eesur
Peas urTc}
Meco
Ca aL
TESA
Cu ect
ET cer ctTEKNOTURF
Attribute Declaration
Attributes/Fields are used to declare the properties of a class, These
Settee eee Ree eC ol oo
They can be intrinsic types (int, boolean...) or user-defined type
Cee eur Tt
* * * [ = ]
Example: private double salary;Method Declaration a
Methods describe the responsibility of the class. These ‘TEMNOTURF
* * ( * ) {
*
}
Example : public boolean isAgeValid() {
ublic void calculateAreaint side) { an
2 iffage>0 && age <=100)
intarea = side * side;
return true;
system.out.printin(area);
; else
return false;
}Create Object =!
eeernaels tas TeRNOTURE
'* Objects are created for a class using the keyword new.
* In Java, all Objects are created at run time in the heap area.
pe ime reference. variable= new class_name()
eens
+ Employee e=new Employee();
Here variable ‘e’ is the.
name of the object. ‘e’ isCreate Object and Access Members
= The attributes and methods of a class can be accessed using the dot
operator as