โ† Back to Courses
ยฉ 2025 sampathirao yoganandh
Week 4 & 5 ยท CO-1

Classes, Objects & Constructors

Classes, Objects, Constructors, Methods, Overloading, Parameter Passing  CO-1

Defining a Class

A class is defined using the class keyword. It is a template containing fields (attributes) and methods (behaviours).

Anatomy of a Java Class

class Employee {
    // โ”€โ”€ Instance variables โ”€โ”€
    int    empId;
    String name;
    double salary;

    // โ”€โ”€ Constructor โ”€โ”€
    Employee(int id, String n, double sal) {
        empId  = id;
        name   = n;
        salary = sal;
    }

    // โ”€โ”€ Instance method โ”€โ”€
    void display() {
        System.out.printf("ID: %d | Name: %s | Salary: %.2f%n", empId, name, salary);
    }

    // โ”€โ”€ Static method โ”€โ”€
    static void companyName() {
        System.out.println("ANITS Engineering College");
    }
}

Creating Objects

Objects are created using the new keyword. This allocates memory on the heap and calls the constructor.

public class Main {
    public static void main(String[] args) {
        // Creating objects
        Employee e1 = new Employee(101, "Ravi", 45000.0);
        Employee e2 = new Employee(102, "Priya", 52000.0);

        e1.display();
        e2.display();
        Employee.companyName(); // static method via class name
    }
}
๐Ÿ“Œ Key PointMemory is allocated on the Heap for objects. The reference variable (e.g., e1) is stored on the Stack.

Constructors

A constructor is a special method that is automatically called when an object is created. Its purpose is to initialize the object's state.

Rules for Constructors

Types of Constructors

TypeDescription
Default ConstructorProvided by JVM if no constructor is defined; initializes fields to defaults
No-Arg ConstructorDefined by programmer with no parameters
Parameterized ConstructorAccepts arguments to initialize fields with specific values
class Box {
    double length, width, height;

    // No-arg constructor
    Box() {
        length = width = height = 1.0;
    }

    // Parameterized constructor
    Box(double l, double w, double h) {
        length = l; width = w; height = h;
    }

    double volume() { return length * width * height; }
}

Constructor Overloading

Having multiple constructors in the same class with different parameter lists is called constructor overloading. It provides flexibility in object creation.

class Student {
    int    rollNo;
    String name;
    double cgpa;

    Student() {                         // No-arg
        rollNo = 0; name = "Unknown"; cgpa = 0.0;
    }
    Student(int r, String n) {          // 2-arg
        rollNo = r; name = n; cgpa = 0.0;
    }
    Student(int r, String n, double g) { // 3-arg
        rollNo = r; name = n; cgpa = g;
    }
    void show() {
        System.out.printf("%d | %s | %.2f%n", rollNo, name, cgpa);
    }
}

public class Test {
    public static void main(String[] args) {
        new Student().show();
        new Student(101, "Arjun").show();
        new Student(102, "Priya", 9.2).show();
    }
}

Method Overloading

Method Overloading = same method name, different parameter list. The compiler differentiates them based on number, type, or order of parameters.

class MathUtil {
    static int    square(int n)    { return n * n; }
    static double square(double n) { return n * n; }

    static int    add(int a, int b)          { return a + b; }
    static int    add(int a, int b, int c)    { return a + b + c; }
    static double add(double a, double b)    { return a + b; }
}
โš ๏ธ ImportantMethod overloading is compile-time polymorphism. Return type alone cannot distinguish overloaded methods โ€” the compiler will throw an error.

Parameter Passing

Java always uses Pass by Value.

void changeVal(int x) { x = 100; }      // original unchanged
void changeObj(Box b) { b.length = 99; } // original object IS changed

this Keyword (intro)

The this keyword refers to the current object. Use it when a local variable shadows an instance variable:

class Circle {
    double radius;

    Circle(double radius) {
        this.radius = radius; // 'this.radius' = instance var, 'radius' = param
    }
    double area() { return Math.PI * this.radius * this.radius; }
}
๐Ÿ“Œ Key Pointthis() can also be used to call one constructor from another (constructor chaining). It must be the first statement in the constructor.
๐Ÿ“„

Download PDF Notes

Save these notes as a PDF for offline reference

View / Download PDF