OOP(s): The Class and Objects

OOP(s): The Class and Objects

Concept

Everything is a Concept & Object.

class_1.png

From the above image, we can see that an abstract concept CAR acts as a class, we can initialize a car with various objects. And here polo, mini, and beetle are objects.

The image above shows how a Car object can be the template for many other Car instances. In the image, there are three instances: polo, mini, and beetle. Here, we will make a new class called Car, that will structure a Car object to contain information about the car’s model, the color, how many passengers it can hold, its speed, etc. A class can define types of operations, or methods, that can be performed on a Car object. For example, the Car class might specify an accelerate method, which would update the speed attribute of the car object.

Another example:

Concept/Class : "Dog" -> Object "Neapolitan Mastiff", "Chow Chow" etc.

dog-class-objects-800.jpg

Class : The Definition

In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). It is a basic concept of Object-Oriented Programming which revolve around real-life entities. Class determines how an object will behave and what the object will contain.

Class: The Syntax

The basic syntax of writing a class in java.

class <class_name>{  
    field;  
    method;  
  }

Example

class Student{
      //Fields
      private String studentName;
      private int studentAge;
      private String studentRollNumber;
      private String studentMotherName;
      private String studentFatherName;

      //Methods
      public String studentDetails(){
          return student_details;
      }
}

Object : The Definition

An object is an instance of a class. An object in OOPS is nothing but a self-contained component that consists of methods and properties to make a particular type of data useful. For example color name, table, bag, barking. When you send a message to an object, you are asking the object to invoke or execute one of its methods as defined in the class.

Object: The Syntax

The basic syntax of creating an object.

ClassName ReferenceVariable = new ClassName();

Example

Student student1 = new Student();
Student student2 = new Student();

Class Vs. Object

Screenshot from 2022-07-27 16-55-47.png

Finally

But while you can always write 'spaghetti code' in a procedural language, 
object-oriented languages used poorly can add meatballs to your spaghetti.

                                                 - Author: Andrew Hunt