CLASS
A class is a template or archetype defined by the user and used to build things. It denotes the collection of attributes or methods that all objects of the same type share. A class functions as a blueprint for creating objects. It's essentially code written by a programmer to outline the qualities and actions of an object.
The qualities are known as attributes, which describe the object and are sometimes called fields due to containing data. Most programmers call them properties, which are placed within the class as public variables or property procedures.
Actions that can be performed on or by the object are operations, also referred to as behaviours, but commonly known as methods. These methods are part of the class's code and are written either as procedures or functions. A class serves as a template for generating multiple objects of the same kind, akin to a pastry cutter creating consistent shapes.
Each object is an instance of a class in the computer's memory. Creating an object from a class is called instantiation. Once objects are formed, their attributes can be assigned values, granting individuality to each object of the same type. The class defines these attributes through property procedures, which may involve code to validate property values during the assignment. This helps maintain the integrity of the contained data.
The combined values of assigned properties on an object are collectively called its state. During object instantiation, values can also be assigned to properties using a special method called new, which acts as the constructor.
OBJECT
An object represents something from the real world, like a car, boat, or book. However, it doesn't have to be a physical item you can touch. It might also refer to non-physical things like a dental appointment, a cinema seat reservation, or a bank account. In the context of object-oriented programming, an object refers to anything important within your software application. It's whatever you need to save and work with data about. Another term for an object is an entity.
So you can say that object is an instance of the class
*Note- the default value of properties of the class will be int rolno = 0 float marks = 0.0 String name = null
now I want you to consider the 1st Image. When executing the compiler goes to class "Student" as per our code then it tries to find object "shahima" Then the value of property rolno, marks, and name respectively is found, if it exists then it's printed. But if the value of the object is not assigned then it prints the default value of the properties in the class. For example for Khushi we have not assigned any value to the various attributes of the class "Student" and hence the compiler will go back and print the default values of the properties of the class.
here is a program to create an object "shahima" of class "Student"
public class Main {
public static void main(String[] args) {
Student shahima = new Student();
Student khushi = new Student();
shahima.rolno = 13;
shahima.marks = 100 ;
shahima.name = "Shahima Khan";
System.out.println("Shahima's data\n");
System.out.println(shahima.marks);
System.out.println(shahima.rolno);
System.out.println(shahima.name );
System.out.println("\nkhushi's data\n");
System.out.println(khushi.marks);
System.out.println(khushi.rolno);
System.out.println(khushi.name);
}
}
class Student{
int rolno;
float marks;
String name;
}
here is the output of our program and it is exactly how we expected it to be.