Classes in Java
Class
A class is a template or a blueprint that has definition for attributes and behaviour of objects of that class. It only provides definition for its objects. A real world example would be, ‘Human’ is a class which defines the basic attributes and behaviour of actual human beings. ‘Tiger’ is another class which defines characteristics of tigers. Similary, Coffee can be a class. In general, for the things (living or non-living) which have common properties and behaviour, we create a class for them.
A class has attributes and methods.
Example of a class:
class Coffee {
//Attributes of Coffee
private String name;
private String colour;
private String type;
//Behaviour of Coffee
public void energizeConsumer(){
System.out.println("The consumer is energized.");
}
}
Another Example of a class :
public class Scientist {
private int id;
private String name;
private String dicipline;
}
Mobile.java class
public class Mobile {
private String name;
private String company;
private int price;
public void displayMobileInfo() {
System.out.println("Name of the mobile : " + name + ", company : "
+ company + ", price : " + price);
}
}