Difference between Class vs Object in OOPS and Java
Here is my list of differences between Class and Object in OOPS. Class and Object are related to each other because every Object must be type of any class. In the same time class itself is of no use until you create object. Let’s see these difference between class and object in points :
1) Class is blueprint means you can create different object based on one class which varies in there property. e.g. if Car is a class than Mercedes, BMW or Audi can be considered as object because they are essentially a car but have different size, shape, color and feature.
2) A Class can be analogous to structure in C programming language with only difference that structure doesn't contain any methods or functions, while class in Java contains both state and behavior, state is represented by field in class e.g. numberOfGears, whether car is automatic or manual, car is running or stopped etc. On the other hand behavior is controlled by functions, also known as methods in Java e.g. start() will change state of car from stopped to started or running and stop() will do opposite.
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Person p1 = new Person("Rakesh");
Person p2 = new Person("Jimmy");
Person p3 = new Person("Peter");
Here Person is a class as it defines design of Person objects i.e. How will a person object look like, what properties it will have etc. By the way Class is declared by keyword "class" in Java and p1, p2, p3 are different object of Person class. In natural language you can say different person which has different names where name is a property of Person Class. Another difference between Class and Object in Java is that we have a class keyword to declare class in Java but there is no object keyword. Objects are most notably created using new() operator, which calls constructor of class to create and initialize object in Java.
That’s all on difference between class and object in OOPS and Java. As I said main difference between class and object is that former is a design while later is actual thing. Class specifies how an object will look like and object belongs to a particular type. In Object oriented programming language you can find real examples of class and object in your surroundings e.g. Home can be a class and everyone’s home can be considered object of class home, because they are home but they are also different to other homes.
0 comments:
Post a Comment