Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Class and Object

Object Oriented Programming is completely based on Objects and Classes.


Java Class

Anything and everthing you see around can be said to be a class. Be it a Car or a Tree or even a Human.

A class can be described as a blueprint which says how an object is going to behave once it is created. And a class has a behaviour and a state.

Sounds Scary?

Let us simplify with the below example :


class Human{

  String name;
  String food;
  String language;

  public void eat(){
    System.out.println("Eats "+food);
  }

  public void speak(){
    System.out.println("Speaks "+language);
  }
}

We have a class named 'Human'. And all of us know humans have a common behavior of speaking and eating. And as said, a class has behaviour and state. We find the two behaviors/methods.



Eat behaviour / method

public void eat(){
   System.out.println("Eats "+food);
}

Speak behaviour / method

public void speak(){
   System.out.println("Speaks "+language);
}


But the language and food varies from person to person. Thus we have the states/variables as : food, language and name as it varies from person to person.


  String name;
  String food;
  String language;

Now, just wait, think for a while. Does the above class Human have any physical existence? I mean Tom, Dick, Harry all are humans and have a physical existance but the above class 'Human' is just a kind of blueprint tellin us how Tom, Dick, Harry is going to behave or react once they are created.

Tom, Dick, Harry are all Objects created from the 'Human class.

Now, does it ring a bell as in why objects are created from a class? Just imagine God has prepared a Human class stating all the behaviours of Human and thus we are behaving right now.

So, class is just a blueprint stating how an object will behave once it is created.


How do we create an Object from a class?

Below is the way we do in java :


class CreateHuman{
  public static void main(String[] args){

    Human human = new Human();
  }
}

Note :'CreateHuman' is the other class where we are going to create objects of 'Human' class.

We have a class called 'CreateHuman', inside which we have the main method(which we will explain later). For now lets get focused on the line inside the main method where objects are created.

The first line is :


Human human = new Human();

This is how we create an object. The name of the object is 'human'(off course you can give it any name).

And on the right hand side we have 'new Human()'. Where 'new' keyword means we are going to create a new object.


Note :In technical terms, 'new Human()' is going to create an instance or object of 'Human' class and in the declaration 'Human human', 'human' is reference variable which is holding the instance/object of 'Human' class.

java_object

So, after we create the 'human' object, a block of memory will be allocated to it. And you are all set to insert values to name, food and language.


Example :

Say you are a Car designer. You have drawn a model of the Car. Based on the model you have drawn the actual Cars are created by workers. Here, the Car model you have drawn is the 'Class' and the actual Cars made by the workers are the 'Objects'.

How do we access the behaviors/methods and properties/state of an Object?

So, we know the 'human' object has three properties/state i.e. name, food and language. And two behaviors/methods eat() and speak(). But how do we access those?


The dot(.) Operator

Behaviors/methods and properties/state of a class can only be accessed using the dot(.) operator of an object.


Accessing the 'eat()' behaviors/method

human.eat();

Where 'human' is the object of 'Human' class. And we have used the dot(.) to access 'eat()'.


Note :We will be leanning next, how to assign values to an Object.