Object
Oriented Concepts:
Class: In Java, a class is a
definition of objects of the same kind. In other
words, a class is a
blueprint, template, or prototype that defines and describes the static
attributes and dynamic
behaviors common to all objects of the same
kind.
A class
can be visualized as a three-compartment box, as illustrated:
1.
Name (or identity): identifies the class.
2.
Variables (or attribute, state, field): contains
the static attributes of the class.
3.
Methods (or behaviors, function, operation): contains
the dynamic behaviors of the class.
·
Local variables: Variables defined inside
methods, constructors or blocks are called local variables. The variable will
be declared and initialized within the method and the variable will be
destroyed when the method has completed.
·
Instance variables: Instance variables are
variables within a class but outside any method. These variables are
initialized when the class is instantiated. Instance variables can be accessed
from inside any method, constructor or blocks of that particular class.
·
Class variables: Class variables are
variables declared with in a class, outside any method, with the static
keyword.
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
void changeCadence(int
newValue) {
cadence = newValue;
}
void changeGear(int
newValue) {
gear = newValue;
}
void speedUp(int
increment) {
speed = speed +
increment;
}
void applyBrakes(int decrement) {
speed = speed -
decrement;
}
void printStates() {
System.out.println("cadence:" +
cadence +
" speed:" +
speed + "
gear:" + gear);
}
}
Object: Object is
an instance of class. An instance is a realization of a
particular item of a class.
To create an instance of a class, you
have to:
1.
Declare an instance identifier (instance name) of a
particular class.
2.
Construct the instance (i.e., allocate storage for the
instance and initialize the instance) using the "new" operator.
Bicycle
b; // Declaration
b= new Bicycle(); //Instantiation
2 Comments
Hi nice content. Sir
ReplyDeleteHi how interesting content
ReplyDelete