Basic Concept of OOP (Object Oriented Programming)

Rumman Ansari   Software Engineer   2022-12-31   16757 Share
☰ Table of Contents

Table of Content:


Object-oriented programming (or OOP in short) is the dominant programming paradigm these days, having replaced the "structured," procedure-based programming techniques that were developed in the early '70s. Java is a true object oriented programming language. Almost everything in Java is an object.All program code and data reside within objects and classes (also called encapsulation).

 

You need to understand some of the terminologies of OOP to go further. The most important term is the class and object.

 

class and object

class and object in java

Class

    • The class is at the core of Java. It is the logical construct upon which the entire Java language is built because it defines the shape and nature of an object. As such, the class forms the basis for object-oriented programming in Java. Any concept you wish to implement in a Java program must be encapsulated within a class.
    • A class is a template or blueprint from which objects are actually made.
    • A class is also defined as a new data type, a user defined type which contains two things 1) Data Member 2) Methods
    • A class defines the properties and behaviors of objects.
    • When you construct an object from a class, you are said to have created an instance of the class.
    • The description of a number of the similar object is also called as a class.
    • Classes are logical in nature.

Example:

    Furniture is a class, and chair, tables are the example of an object, Furniture does not have any existence but the chair, table do exist. So we can say that Classes is logical in nature.

Syntax for Class

class Circle {
   String radius;
   
   void  getArea(){
   
   }
   
  void  getPerimeter(){
      
   }
}

Explanation of program syntax:

class and object

Object

  • An object is an instance of a class.
  • Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified.
    For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects.
  • Object to object communication is done via methods.
  • Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods.


An object has a unique identity, state, and behavior.

  • The state of an object (also known as its properties or attributes): is represented by data fields with their current values.
    A circle object, for example, has a data field radius, which is the property that characterizes a circle. A rectangle object has the data fields width and height, which are the properties that characterize a rectangle.
    A dog has states -- color, name, breed
  • The behavior of an object (also known as its actions): is defined by methods. To invoke a method on an object is to ask the object to perform an action.
    For example, you may define methods named getArea() and getPerimeter() for circle objects.
    A dog has behaviors -- wagging the tail, barking, eating.
  • The identity of an object: is defined the object distinguished from others that may have the same behavior and state?

Different ways to create an object in Java

There are many ways to create an object in java. They are:

  • By new keyword
  • By newInstance() method
  • By clone() method
  • By deserialization
  • By factory method etc

These are simple way to create object

Way 1: Creating Object

Calculation c=new Calculation();  
c.cal();  

Example

 
class calculation{
  void cal()
   {
   double a =4, b=23, c=44;
   double d;
   d = (c+b)/a;
   System.out.println(d);
   }
 }
 
 class MainClass{

 public static void main(String args[]){
  calculation c=new calculation();  // creation of object
   c.cal();
 }
} 

Output

16.75
Press any key to continue . . .

Way 2: Creating Object

new Calculation();//anonymous object

Example

  
class calculation{
  void cal()
   {
   double a =4, b=24, c=44;
   double d;
   d = (c+b)/a;
   System.out.println(d);
   }
 }


 class MainClass{

 public static void main(String args[]){
   new calculation().cal();;  // creation of object
 }
}

Output

17.0
Press any key to continue . . .

Way 3: Creating Object

new Calculation().cal(6);  

Example

  

class calculation{
  void cal(double p)
   {
   double a, b=32, c=44;
   double d;
   a=p;

   d = (c+b)/a;
   System.out.println(d);
   }
 }


 class MainClass{

 public static void main(String args[]){
   new calculation().cal(6);;  // creation of object
 }
}

Output

12.666666666666666
Press any key to continue . . .

Creating multiple objects by a single statement

Rectangle r1=new Rectangle(), r2=new Rectangle();//creating two objects

Example

class MainRectangle{
 public static void main(String args[]){
  Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objects
  r1.insert(10,5);
  r2.insert(4,15);
  r1.calculateArea();
  r2.calculateArea();
  }
}

class Rectangle{
 int length;
 int width;
 void insert(int l,int w){
  length=l;
  width=w;
 }

 void calculateArea(){
	 System.out.println("Area = "+length*width);
  }
}

Output:

Area = 50
Area = 60
Press any key to continue . . .

  More about Class and Object