Concept Of Class and Object in Java With Examples

The most important part of Object-Oriented Programming is to make the concept of Objects and Classes very clear.
Java is an Object-Oriented Programming Language, Which helps us to increase the flexibility and maintainability of the program.
Java is known as an OOP because we need to create classes and objects here.
We will cover up below contents

  • What is Object in Java?
  • What is Class in Java?
  • Real Life example of classes and objects
  • Relating Real-life examples of classes and objects with Java Programming Language
  • Example of making classes and objects in java language

before going to understand what is class in java it is better to understand what is an object in java

What is Object in Java?

Formal definition: Object is an Instance of a class. (Though we have not gone through class yet so we will make easier words to elaborate Object)

Like every object in real life, In Java, we have object which holds some states and behaviors. States are stored in fields and behaviors are shown via methods.
There is another thing in an object which is known as identity.

Identity is nothing but a unique name of an object which helps us to make the interaction of one object with another object(s).

What is Class in Java?

A class is simply a blueprint or we can say a template from which we create objects.

Object---->>>>instance of------->>>>>class
Class----->>>>template of------>>>>object

Class generally represents the common properties of all objects of one type.
A class can contain local variables,instance variables and class variables.
Local variables are those variables which are defined inside methods, constructors or blocks.
Instance variables are those variables which are defined outside the methods, constructors or blocks but inside the class.
Class variables are very similar to instance variables but the difference is that this type of variable is defined using static keyword.

A variable holds some kind of data and a method is a function which helps you to do something with the variable.
the object holds both variables and methods.
class is the blueprint where you describe the methods and variables.



Real Life example of classes and objects

The best way to give a real-life example of class and object is to consider you in this example. Yes, it’s you.
Who are you what is your identity? You are a person, right?

(an object is a thing and a class is a category of things)

So just think it
A person is a class.
And “you” are an object of that person class.

and to define you there will be some properties those are: height, weight, age, sex, name

Relating Real-life examples of classes and objects with Java Programming Language

Let’s take a Bird class. It has color, weight, breed those are known as state and behavior will be – flying, eating etc

Now if we try to figure it out with Java programming both are almost similar by characteristics. Like real life here in programming, we can also have state and behaviors. Here states are stored in fields and methods will point out the behaviors.

Example of making classes and objects in java language

 

How to create object of a Java class?

example of a simple class

public class Person {
   String name;
   int age;
   String sex;

   void walking() {
   }

   void dancing() {
   }

   void eating() {
   }
}

Example of creating objects inside a class using constructor:

public class Person {
   public Person(String name) {
     
      System.out.println("person name is:" + name );
   }

   public static void main(String []args) {
      
      Person person1 = new Person( "faruque" );
       Person person2 = new Person( "saruque" );
      
      
   }
}

Output:

run:
person name is:faruque
person name is:saruque
BUILD SUCCESSFUL (total time: 0 seconds)


Leave a Reply

Your email address will not be published. Required fields are marked *