Wednesday, February 12, 2020

Difference between Class and Object in Java and OOPS with Example

Oracle Java Tutorial and Material, Oracle Java Certification, Oracle Java OOPs, Oracle Java Prep

Class and Object are two most important concept of Object oriented programming language (OOPS)  e.g. Java. Main difference between a Class and an Object in Java is that class is a blueprint to create different objects of same type. This may looks simple to many of you but if you are beginner or just heard term Object Oriented Programming language it might not be that simple. I have met many students, beginners and programmers who don’t know difference between class and object and often used them interchangeably. Also Java API having classes like java.lang.Object and java.lang.Class also adds more confusion in beginners mind. Both of them are totally different things, class and object in OOPS are concepts and applicable to all Object oriented programming language e.g. C++ or Scala. On the other hand java.lang.Class and java.lang.Object are part of Java API. Along with other OOPS concepts like Abstraction, Encapsulation, Inheritance and Polymorphism, this is also one of the most fundamental of Object oriented programming (OOPS) which needs to be clearly understood before proceeding into serious application programming. Without clear understanding of Class and Object you are more prone to make errors, not able to comprehend an already written program and it would be pretty hard for you to find bugs or fix errors or exceptions in Java code.  In this article we will look this on different angles to differentiate Class and object in Java.

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.

Oracle Class, Oracle Object in Java, Oracle OOPS, Oracle Java Tutorials and Material

3) Object is also called instance in Java and every instance has different values of instance variables. e.g. in following code

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.

Related Posts

0 comments:

Post a Comment