☞Class is a blueprint or template for its objects.
☞It is a description of object’s property(characteristics) and behavior.
☞It is a logical entity and cannot be physical.
class Square{ int side; void input(int s){ side=s; } void area() { System.out.println("Area is "+(side*side)); } void perimeter() { System.out.println("Perimeter is "+(4*side)); } }
☞Object is a real world entity.
☞It is an instance (example) of a class.
☞It consumes memory to hold property values.
☞An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical.
☞Creating an object: use new keyword
Square s1=new Square();
class Square{
int side;
void input(int s){
side=s;
}
void area() {
System.out.println("Area is "+(side*side));
}
void perimeter() {
System.out.println("Perimeter is "+(4*side));
}
public static void main(String[] args) {
//creating an object
Square obj= new Square();
obj.input(5);
obj.area();
obj.perimeter();
}
}
Area is 25 Perimeter is 20