Class : Used to define a real life entity into a programming environment.
Any real life entity having at least one property
and a corresponding behaviour
can be considered as a class. Lets take an example of a Car, it is having a property accelerator
which helps the car to move and to control its speed. The corresponding behaviour is acceleration
, which is directly proportional to the push applied to the accelerator.
class Car {
private String tier;
private String tierFriction;
private double weight;
private double gasFedToEngine;
}
The above class shows some properties of a Car, on which its acceleration depends. Behaviour (method in the class
) always depends on the property(s) (global attribute(s) of the class
). Now if you want more details you can define Tier
as another entity, then the definition will look like
class Tier {
private String tierMaterial;
private String tierFriction;
private double weight;
private double tierDiameter;
}
class Car {
private Tier tier; // getting all properties of Tier here itself
private double weight;
private double gasFedToEngine;
}
Object : Used to define various flavours of an Entity and to perform data manipulations on them separately.
Now we have defined an entity to our program, say we are having a showroom of used cars, having cars of different companies. So each car becomes an object
of our entity. Objects can be Audi, Nissan, Ferrari etc. So after opening the showroom we add cars to it like this
static List<Car> showroomCars = new ArrayList<Car>();
public boolean addCarToShowroom() {
Car carNissan = new Car(); // always creates a new objects and allocates some memory in heap
carNissan.setName("Nissan");
carNissan.setColor(RED);
carNissan.setWeight(300);
showroomCars.add(carNissan);
Car carAudi = new Car();
carAudi.setName("Audi");
carAudi.setColor(BLACK);
carAudi.setWeight(270);
showroomCars.add(carAudi);
}
So now two new cars are added to the Showroom, one of Nissan and another one of Audi, each having their own specific attribute values.
Class
only gives definition, manipulation is done on Object
, for doing manipulation on any Class, object should be created. Every time an object is created to a class all its non-satic(instance) variables will load into memory with their respective default values.
Reference : Used to address an object
When we say Car carAudi = new Car();
we define a new object to Car and a memory location is assigned for that object. The reference variable carAudi
holds the memory address of that object. Object is never accessed directly by user, neither its memory location. That's where reference variable has significance, its stores the hexadecimal
format of memory location. If we want to do modifications on an object do with the help of reference not directly.
An object can have any number of reference, but a reference can point only to one object at a time.
class Car {
void test() {
Car car1 = new Car(); // (1)
Car car2 = new Car(); // (2)
car2 = car1;
/**
Says that car2 should point to where car1 points, so now both points to first object of Car
But for this car2 has to loose its current object-(2), making it an Abandoned object (An object with no active reference from the stack).
**/
}
}