Difference between Object and Class in Java
Understanding Object and classes in java are very important.This article will explain into details the difference between Object and a Class.
The object has both logical and physical entity whereas Class contains only logical entity.
Generally, a Object has 3 xstics.
The object has both logical and physical entity whereas Class contains only logical entity.
Object
Generally, a Object has 3 xstics.
- State-This is object data.
- Behaviour-This is object functionality e.g delete, deposit etc.
- Identity -each object has a unique identity which is not visible to the user.
Object is normally an instance of a class.Class is a template from which we are able to create new objects.
Class
Class is a group of objects with similar xstics.A class can contain-:
- Fields
- Methods
- Constructors
- blocks
- nested class and interface.
New keyword is used to create a new Object.
Syntax for creating a class
class{ field; method; }
Example code showing class and object
public class Account {
public String name;
public int id;
public double balance;
public void setName(String n)
{
name=n;
}
public String getName()
{
name=this.name;
return name;
}
public void setID(int idnumber)
{
id=idnumber;
}
public int getID()
{
id=this.id;
return id;
}
public void setBalance(double bal)
{
balance=bal;
}
public double getBalance()
{
balance=this.balance;
return balance;
}
Account()
{
setName("Eric Mutua");
setID(27958503);
setBalance(1234.50);
System.out.println("Account Details");
System.out.println("=====================================");
System.out.println("Account Name "+getName() );
System.out.println("ID Number "+getID() );
System.out.println("Balance "+getBalance() );
System.out.println("=====================================");
}
public static void main(String args[])
{
new Account();
}
}