Programming Session Day 1 Blog: Understanding Classes and Object-Oriented Concepts


What is a Class and Why is Class a Data Type?

In Java and other object-oriented languages, a class is a user-defined blueprint or prototype from which objects are created. It is a logical structure that defines variables (states) and methods (behaviors).

A class is considered a non-primitive data type because it can store multiple values (states) and methods (behaviors). It is not a simple single-value like int or char, but rather a complex structure that helps in building real-world entities in code.

In Java:

  • A class is a logical template.
  • An object is a physical instance of that class in memory.
class Car {
  String color;
  int speed;
}

Car myCar = new Car(); // Object creation

Primitive vs Non-Primitive Data Types

Language Primitive Data Types Non-Primitive Data Types
C int, float, char, double Arrays, Structs
C++ int, float, char, bool, double Class, Object, Array, String
Python int, float, str, bool List, Tuple, Dict, Set, Object
Java byte, short, int, long, float, char, boolean Class, String, Array, Object

Primitive Data Types are the basic building blocks provided by the language to hold simple values.

Non-Primitive Data Types are created by the programmer to hold multiple values or complex data, and they often include built-in types like Arrays and Strings or user-defined types like Classes.


Class is Logical, Object is Physical

  • Class: A logical structure or design.
  • Object: A real, physical entity created using the class blueprint. It occupies memory during runtime.

Memory is only allocated when an object is instantiated using new keyword.


Java is Not 100% Object-Oriented

Java supports primitive data types (like int, char, float, etc.) which are not objects. This makes Java not 100% object-oriented.

Languages that are considered 100% Object-Oriented:

  • Smalltalk
  • Ruby
  • Scala

These languages treat everything as objects, including numbers, functions, and control structures.


Object Has State and Behavior

  • State: The data/values held by the object’s variables.
  • Behavior: The functionality/methods that the object can perform.
class Student {
  String name; // state
  int age;     // state

  void study() { // behavior
    System.out.println(name + " is studying");
  }
}

Variable Has State, Not Behavior

A variable is used to hold data (state). It does not have any behavior. Only methods can define behavior in programming.


State vs Behavior

Category Definition Example
State Stored information name, age, color
Behavior What the object can do drive(), study(), speak()

Why Class is Non-Primitive Data Type

A class can:

  • Store multiple variables (state)
  • Contain multiple methods (behavior)
  • Be used to create multiple instances (objects)

Hence, a class goes beyond simple data storage like primitive types and provides a structured way to model real-world entities.


What is Object Cloning?

Object cloning is the process of creating an exact copy of an existing object in memory. It is used when a duplicate object with the same state is required.

class Person implements Cloneable {
  int age;

  protected Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
}

To clone an object:

  • The class must implement Cloneable interface.
  • The clone() method must be overridden.

What is a Parallel Language?

Parallel language refers to programming languages or environments that support parallel processing — where multiple processes are executed simultaneously to improve performance and efficiency.

Examples:

  • C with OpenMP or MPI
  • Python with multiprocessing
  • Java with multithreading

What is a Procedural Language?

A procedural programming language focuses on the sequence of steps (procedures) to be carried out. Code is organized as a set of procedures or routines.

Examples:

  • C
  • Pascal
  • BASIC

Characteristics:

  • Focus on functions and procedures
  • Code execution in sequence
  • Variables are accessible throughout the procedure

What is Object Reference and Class?

An object reference is a variable that holds the memory address of the actual object. It does not hold the object itself.

Car c = new Car(); // c is an object reference

Here, c is not the object. It's a reference pointing to the memory where the object is stored.


What is a Statement in Java?

A statement is a complete unit of execution in Java. It ends with a semicolon (;). Statements tell the compiler what action to perform.


Types of Statements in Java

  1. Normal Statements
    • Regular commands like variable declarations and assignments.
int a = 5;
   System.out.println(a);
  1. Conditional Statements
    • Executes code based on a condition.
if (a > 0) {
     System.out.println("Positive number");
   }
  1. Control Flow Statements
    • Manages the flow of execution.
      • Looping: for, while, do-while
      • Branching: break, continue, return
for (int i = 0; i < 5; i++) {
     if (i == 3) continue;
     System.out.println(i);
   }

This blog provides a solid foundation on the basics of classes, objects, types, and structures in programming. These concepts are the stepping stones toward mastering object-oriented programming. Stay tuned for Day 2!