Classes And Objects Are Essence Of Java

Object Oriented Programming 101 — Classes and Objects

Java — Classes and Objects

SUMIT SHARMA
Geek Culture
Published in
5 min readApr 25, 2022

--

Object Oriented Programming Concepts — Classes and Objects

OOP has been widely successful. Java being an Object Oriented Programming Language, Classes and Objects are really the essence of Java. Here in this article we are going to understand and learn about two important Object Oriented concepts — Classes and Objects in Java. These are actually the most basic concepts of Object Oriented Programming which we use to model real-life entities. Many young programmers fail to explain/answer, that is why I am writing this article. This article is only for the audience who knows OOP theoretically but unable to link it with real world and programming world.

I think the best way to understand the concept of Classes and Objects is by looking at an example — of simple Java class representing a Car.

Car as a Class and Its Objects

To understand more let us consider that our Car class have some properties like type, model, color and speed. As said in the introduction section that these concepts are used to model the real-life entities. Here Car is a logical concept and now we can use this Car class to build some specific type of Objects which has physical presence in real world. Here below Java class represents the blueprint of Car class in general, It is a kind of template we can say, which we use to create real objects.

Syntax of Class in Java -

class <Class Name> {
field;
method;
}
class Car {
// fields or properties
String type;
String model;
String color;
int speed;

// methods
// Constructors
Car(String type, String model, String color) {
this.type = type;
this.model = model;
this.color = color;
}
}

Here, based on the above discussion I have written some lines about Class —

  • Class is a blueprint or template or prototype or logical construct or set of instructions to build specific type of objects.
  • Class does not have physical presence (does not really exist in real world & does not occupy memory space in programming world), it is only a set of rules or definition.
  • Class is a generic definition of a real life entity.
  • Class actually determines How an Object will behave and What an Object will contain (Class describes the state and behavior of an object).
  • In the Class file we represent State or Properties as fields and Behavior with methods.
  • We can use Class for creating the user-defined data types.
  • Classes are translated during Compile Time.
Car Class — Looks Like a Template or Blueprint

Now by using above template or blueprint of Car class we can create any type of Car in real. Like in real world using this template or blueprint so many companies created their own cars — Maruti, Audi, BMW etc. The cars created by these companies have all these properties (type, model, color, speed) but the values can be different.

Object — Implementation of its Class

Below is the code for creating the different car objects from class Car. Now we have three car objects all of them are created from the single class.

Object Syntax in Java —

ClassName ReferenceVariable = new ClassName();Car focus = new Car("Maruti", "Focus", "red");
Car golf = new Car("Audi", "Golf", "blue");
Car auris= new Car("BMW", "Auris", "green");

Here, based on the above discussion I have written some lines about Object—

  • Object is an instance or implementation of a Class.
  • Object is a real time entity (or run time entity) that has states and behaviors.
  • Objects are created from Classes at runtime.
  • Object has three estential properties —
    - State — represents data (value) of an object. [Variables]
    - Identity — object identity internally maintained by JVM to identify each object uniquely.
    - Behavior — represents the behavior (functionality)of an object (It is an effect of data type operations) [Methods]

Real World Examples —

Example -1
Let’s suppose we want to construct a house, then we create some blueprint or construction plans before actually starting building the house. But at the end we create the house which is actual object created from blueprint.

Example -2
Let’s convert the real life entities dogs into software objects. Below we have three different breeds of dogs.

To identify the data members of our object we will look for differences between them.

Dog Objects

Some of the differences we might observe are — breed, age, color. If we think for a moment these differences are also the common characterstistics shared by these dogs.

Common Characteristics

Now we are going to list out the common behaviors like sleep, sit, eat, run. These will become the actions of our software object.

Common Behaviors

So we have identified below things -
Class — Dogs
Data Members — Breed, Age, Color
Methods — Eat, Sleep, Sit, Run

Now for different different values of these data members (Breed, Age, Color) in Java Class, we will get different dog objects.

Different Dog Objects

Conclusion —

Following the above article it will be easier to develop a good understanding of Object and Class in OOPs. We can create any Object from its Class using this approach and relate the real world entity to programming world.

--

--

SUMIT SHARMA
Geek Culture

Software Development Engineer, Stock Market Analyst, Fitness Coach, Video Editor, Freelancer