Java Class and Object

Java Class and Object

Java is an object-oriented language. Object-oriented means, Java works with real-world objects. Its just representation of real-world objects to programming based objects.  Suppose we can think a dog is an object. We will represent it with our programming language. So, the dog has some state and behaviors. Breed, age, size, color are some states of a dog and eat, run, sleep are some behaviors of a dog. We will represent this state and behaviors with programming language.

What is class?


As we just discussed, a real-world object has some state and behaviors.  Classes are the blueprint of these objects. Class contains a set of instructions that means the state or behavior of a real-world object. Suppose Dog is a real-world object that can be represented through the java class with below lines of code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class Dog{
 //states
 int size;
 int age;
 String color;
 //constructor
 Dog(){
  size = 30;
  age= 2;
  color = "RED";
 }
 //behaviors
 public void eat(){
  System.out.println("Dog eats meat.");
 }
 //main method
 public static void main(String[] args){
  new Dog().eat();
 }
}
On the above example, Dog is a class and its states are size, age, color and behavior is eat. This is the blueprint of a real-world object dog with programming language java. Java defines some syntax to represent a real-world object with java class. So class syntax will be like the below code.

1
2
3
4
class CustomClass {
    // field, constructor
    // method declarations
}

But In our dog class, we can see that we have define public keyword before the class and also add a Dog() method and the static main method. We will discuss this thing later. A class can contain many fields and methods. A java class contains the following state and behaviors :

Fields: In the above example, we can see that variables are commented as state of the class. Yes, all kinds of variables are the state of the class. These variables are the properties or fields of a class. We should declare fields within a class.

Methods: Methods define the behaviors of a class. For example, In Dog class, we have created a method called eat() which means it will defines the eating behavior of a real-world dog.

Constructors: Constructor is used to instantiate a class. When we create a class object using new keyword, we have to call the constructor after the new keyword. Like the above example,  we instantiated a Dog class using new Dog(). Then we can access the eat() method from the main method. Constructor can be parameterized or empty. Constructor that does not contain any parameter is called default constructor. Every class has an empty constructor by default. In Dog class, if we remove the Dog() constructor, it still has this constructor.

Blocks: Java class can contain a group of statements enclosed by braces.

Nested classes: Java class can also contain another class inside of it. These classes are called nested class.

What is Object?

Object is an instance of a class. In the above example, we have created a Dog class and access this class method from the main method by creating an instance of the Dog class using the new keyword and Dog constructor. Then we have accessed the eat method of the Dog class. When we created a dog class instance JVM allocates heap memory location for this class object. For every variable and methods of a class will take some reference on memory. We will discuss about JVM and memory allocation later. Every instance of a class has its own identity. If we write more code to the above example we should see the following code.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Dog{
 //states
 String name;
 int size;
 int age;
 String color;
 //constructor
 Dog(String name, int size, int age, String color){
  this.name = name;
  this.size = size;
  this.age= age;
  this.color = color;
 }
 //behaviors
 public void eat(){
  System.out.println(this.name + " eats meat.");
 }
 //main method
 public static void main(String[] args){
  Dog dog1 = new Dog("ABC",23,3,"white");
  Dog dog2 = new Dog("XYZ",24,4,"black");

  dog1.eat();
  dog2.eat();
 }
}

The above code creates two objects of dog class which is not the same as one another. dog1 has some different identity from dog2. Object creation follow the below steps:

Declaration: In the above code we first declare dog1 and dog2 variable.
Instantiation: The we instantiate the Dog object with new keyword.
Initialization: Then with the parameterized constructor, we have initialized every object which has own identity.
      
   
Reactions

Post a Comment

0 Comments