Java is a object Oriented Programming language. Programming deals with defining objects states and behaviors.
In Java, Objects are logical representation of a real world objects or a conceptual thing.
Class is a template, which defines the structure of the Object and its behavior.
Using a class, we can create any number of objects.,
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Class Vehicle { int numberOfWheels; int numberOfDoors; String vehicleName; public void startEngine() { System.out.println("Execute ON Engine Command"); } public void shutDownEngine() { System.out.println("Execute OFF Engine Command"); } } public class Driver { public static void main(String args[]) { Vehicle car = new Vehicle(); Vehicle bike = new Vehicle(); } } |
The above example Vehicle is a class, which has the following properties & behaviors.
Properties:
numberOfWheels
numberOfDoors
vehicleName
Behaviors:
startEngine()
shutDownEngine()
In the Driver class, we have created 2 objects car & bike using the Vehicle Class.
However the car & bike has its own different properties (states) e.g,
numberOfWheels for car Object is 4, and numberOfWheels for bike object is 2.
Also both car and bike may have differents behaviours based on their usage.,