Object in Java
Java is a logical entity only.
![]() |
oject in java |
An object has three characteristics:
- State: represents
the data (value) of an object.
- Behavior: represents
the behavior (functionality) of an object such as deposit, withdraw, etc.
Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.
![]() |
object in java |
An object is an instance of
a class. A
class is a template or blueprint from which objects are created. So, an object
is the instance(result) of a class.
Object Definitions:
- An object is a real-world entity.
- An object is a runtime entity.
- The object is an entity which has state and behavior.
- The object is an instance of a class.
Example of Object:
![]() |
object in java exemple |
Declaring Objects (Also called instantiating a class)
![]() |
object in java example |
Initializing an object
//
Class Declaration
public
class Dog
{
//
Instance Variables
String
name;
String
breed;
int
age;
String
color;
//
Constructor Declaration of Class
public
Dog(String name, String breed,
int
age, String color)
{
this.name
= name;
this.breed
= breed;
this.age
= age;
this.color
= color;
}
//
method 1
public
String getName()
{
return
name;
}
//
method 2
public
String getBreed()
{
return
breed;
}
//
method 3
public
int getAge()
{
return
age;
}
//
method 4
public
String getColor()
{
return
color;
}
@Override
public
String toString()
{
return("Hi
my name is "+ this.getName()+
".\nMy
breed,age and color are " +
this.getBreed()+","
+ this.getAge()+
","+
this.getColor());
}
public
static void main(String[] args)
{
Dog
tuffy = new Dog("tuffy","papillon", 5,
"white");
System.out.println(tuffy.toString());
}
}
|
No comments:
Post a Comment