Classes in Java
By
Black-clouds
Trying to put smile on your face
Introduction
The entire concept of Object Oriented Programming is not about
objects, its about class. Because we use classes to create objects…..
The class describes what an object will be.
It’s a blue print of an object and it is the
description and definition of an object.
Blue print example
Creating Object
Using blue print (class) we create object
One class, multiple objects….
Once a class is created, using single class we can create any number of objects.
How to Create a class?
To create a class first, we need to find the following….
typ
Name - What is it ? e
Student, Employee, BankAccount, Document, Car, Box…..
Properties,
Attributes -What describes it ? data
name, salary, balance, size, color, width…..
operatio
Behavior - What can it do ? ns
read, play, search, save, create, print, close, open, deposit
Example class
Creating a Bank Account class
Name - BankAccount
Attributes -
name, AccNo, balance, dateOpened, accountType, branch
Behavior open(
- ), close( ), deposit( ), withdraw( )
Example class
A class is represented in a rectangle box with 3 rows..
ClassName
BankAccount Rahul Asha Jayasree
12345 54321 67894
List
name Creating
1500
3/5/2014
1800
3/6/2013
15000
23/5/2014
Of
accountNumber
Attributes
balance objects is
Savings
Sec’bad
Savings
HYD
Current
Sec’bad
.dateOpened
.accountType called
open() open() open()
branch
instantiation
close()
deposit()
close()
deposit()
close()
deposit()
List withdraw() withdraw() withdraw()
open( )
Of
close( )
Operations RahulAcct AshaAcct JayaAcct
deposit( )
.
withdraw( )
.
class Object (instance)
Creating object of a class
Syntax for object creation
ClassName objectName = new
class BankAccount
BankAccount
ClassName( );
{
nameString name ;
int accountNumber;
accountNumber
balance
double balance ; Example for object creation
dateOpened
String dateOpened;
accountType
String accountType; BankAccount rahulAcct = new
branch
String branch; BankAccount( );
BankAccount ashaAcct = new
open(
void
) open( ){….} BankAccount( );
void
close( ) close( ){….}
deposit(
void )deposit( ){….} BankAccount jayaAcct = new
withdraw(
void withdraw(
) ){….} BankAccount( );
}
}
Class Structure in programming….
class BankAccount
BankAccount
{
nameString name ;
int accountNumber;
accountNumber
balance
double balance ;
dateOpened
String dateOpened;
accountType
String accountType;
branch
String branch;
open(
void
) open( ){….}
void
close( ) close( ){….}
deposit(
void )deposit( ){….}
withdraw(
void withdraw(
) ){….}
}
}
Classes in
Next….
Java Programming