Basic Concepts of OOPS, Class, and Object in Java -


Object Oriented Programming System (OOPS) is based on the concept of Objects. To solve a problem in OOPS (or we can say - to develop any software using OOPS), the first step is to identify the objects in problem, and then build data (variables) and functions (methods) around these objects.

Object -

Object is an entity that may represent a human being, a place, a thing or any other item required to solve a problem. Objects may contain Data (variables in programming) and functions (methods) associated with them.

Object = Data + Methods
An object can access the data associated with itself only, and objects can communicate with each other using functions(methods) -
Objects communication using functions fig: Objects communication using functions.

Class -

A class is a collection of objects of similar types. It is a container, for example 'Student' is a container for different students in a class. In this case particular student, for example Jerry, will be object of class Student.

Syntax for declaring a class -
class <Class_Name>
{
  // body of class
} 
For Example -
class Student
 {
   // Variables
   // methods etc. 
 }

Creating Object in Java -

Java provides a keyword named new to create objects.It allocates memory for objects in Heap data structure at runtime.
Syntax-

Class_Name Object_Name = new Class_Name()         
Where -
Class_Name - is the name of class of which we want to create object.
Object_Name - is the name of object.

For Example - For a class named 'Student', we can create object as follows -
Student st = new Student()         
Where -
Student - is the class name.
st - is the object name.

Class does not have physical existence only objects exist physically :-

Class is a logical entity, it does not have physical existence. We can define it in two ways :-

1- In General -

Consider a class 'Animal'. There is nothing exists in the word physcially with the name Animal, it is just a class used to define different entities (Objects) such as Tiger, Cow etc. Only these entities exist physically in the real world.

Class and its objects with data and functions Fig:- Class and its objects with data and functions

In the above figure Animal represents a class which contains objects named Tiger, Cow, Lion and Bear (all of same type means belongs to animal category). And each object may have data and methods associated with them.

2- In Programming-

Technically it means that - no memory is allocated for classes whatever data members it contains, memory is allocated to objects only.
For example consider the following class-
class Students
 {
  int id, marks;
 }
Here class Demo contains 2 integer variables; in java an integer takes 4 bytes in memory, so you would thing that Student class will take 8 bytes of memory. But no memory will be allocated to these variables, until we create objects of this class.
So as soon as we write the following line of code -
 Student st1 = new Student();
compiler will allocate 8 bytes of memory space to object.

If we create one more object of class -
 Student st2 = new Student();
again it will allocate 8 more bytes of memory to created object.