Professional Documents
Culture Documents
Example:
A dog has states - color, name, breed
and behaviors – wagging the tail, barking, eating.
Class dog
{
String color; // declared next to define
String name;
String breed;
wagtail();
bark();
eat();
}
Class myProject{
Class specialDog()
{
int cost;
String color; // declared next to define
String name;
String breed;
wagtail();
bark();
eat();
}
specialdog( )
{
int Cost = 50,000;
string name = “labrador”;
string eat=”foodstuff”;
}
Member var;
Main( )
Classname xyz1;
//Classname xyz2;
//Classname xyz3;
this.abc( );
When an object of a class is created, the class is said to be instantiated.
All instances share the attributes and the behavior of the class.
values of those attributes, i.e. the state are unique for each object.
This tells the compiler that we will use name to refer to data whose type is of
type.
Example
Dog lab;
The new keyword instantiates a class by allocating memory for an object and
returning a reference to that memory.
Example
// Constructor declaration
//same name as Class name
//variables are initiliased in the constructor
String login;
String pwd;
login( );
Constructors are used for initializing new objects. Fields are variables that
provides the state of the class and its objects, and methods are used to
implement the behavior of the class and its objects.