Java Basics - Lecture 4 - What is class and object in Java

What is object and class in Java

Java Class and Object

This lecture will cover basic OOP concepts. What is Class and Object? What is the difference between them?

Lecture 4 Agenda


1) Class
2) Object

Classes and objects are the fundamental parts of OOP. In OOP, we try to map real-world entities into software objects. Although there is a confusion between object and class, in this article, we will try to explain the difference between object and class. First, let's understand what they are.

What is a class?

A class is a blueprint or template for objects. It defines the behavior of objects. It determines how an object will behave and what the object will contain. In other words, a set of instructions to build a specific type of object.

Syntax:

public Class {
 Data Members;
 Member Functions;
}

Class Components:

Access Modifiers: A class can be public or has default access.
Class Name: The class name begins with a Capital letter.
Class Body: The class body is shown by curly braces, { }. It contains data members and member functions.

What is an Object?

An object is an entity with some attributes and behavior. When we look around, we see many examples of real-world objects: dog, desk, television, bicycle, etc. The real-world objects have two characteristics one is called "state" and the other is "behavior".

For example, dogs have state (name, color, breed, age, size) and dogs have behavior (barking, eating, running, sleeping). In terms of Software objects, they also have state and behavior.

Remember: To create an object of a class"new" keyword is used.

Syntax:
ClassName ReferenceVariable = new ClassName();

An object consists of :

State: It is represented by attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by methods of an object. It also shares the response of an object with other objects.
Identity: It assigns a unique name to an object and enables one object to interact with other objects.

When an object of a class is created the class is called instantiated. All the instances share the attributes and the behavior of the class, the state will be different for each object. A single class can have more than one instance. As shown in the below image.

What is object and class in Java

Let's take an example of a class Car. Below is the Java code for this class.
//Class Declaration

public class Car {
 // Instance Variables
 int model;
 int wheel;
 String color;
 // Member Function
 public String getInfo() {
  return ("Modal is: " + model + " Wheels are: " + wheel + " Color is: " + color);
 } 
 public static void main(String[] args) {
  Car car = new Car();
  car.model = 2015;
  car.wheel = 4;
  car.color = "while";
  System.out.println(car.getInfo());
 }
}
Output: 

Modal is: 2015 Wheels are: 4 Color is: white

In the above example, we created one reference variable (car) and assigned a new instance of the Car class to this variable. Such an object is also called an instance of the class.

Summary:

A class is a user-defined blueprint or template from which objects are created. It determines how an object will behave and what the object will contain.
A Java object consists of methods and properties to make a certain type of data useful.
A class system allows the program to define a new class (child class) in terms of an existing class (parent class) by using a technique like inheritance, Polymorphism, and overriding.

0 comments:

Post a Comment