Java Constructors (Default + Parameterized)

Java Constructors: Default and Parameterized (With Examples)

✨ Quick Summary

In this post, you’ll learn what Constructors are in Java and why they are used. We will cover default constructors, parameterized constructors, and how constructors help in initializing objects with simple examples.

Java OOP Constructor OOP Concepts

In our previous post, we learned about Java OOP and understood the basics of Class and Object.

Now it’s time to learn an important OOP concept: Constructors.


1) What is a Constructor?

A constructor is a special method used to initialize objects in Java.

It is called automatically when an object is created.

Example:


Student s1 = new Student();

When this line runs, the constructor gets called automatically.


2) Constructor Rules in Java

  • ✅ Constructor name must be same as the class name
  • ✅ Constructor does not have a return type (not even void)
  • ✅ Constructor is called automatically when object is created

3) Default Constructor Example

A default constructor is a constructor without parameters.


class Student {

    Student() {
        System.out.println("Default Constructor Called!");
    }
}

public class DefaultConstructorExample {
    public static void main(String[] args) {

        Student s1 = new Student();
    }
}

4) Parameterized Constructor Example

A parameterized constructor is a constructor with parameters.

It allows you to set values while creating an object.


class Student {

    String name;
    int age;

    Student(String n, int a) {
        name = n;
        age = a;
    }

    void display() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

public class ParameterizedConstructorExample {
    public static void main(String[] args) {

        Student s1 = new Student("Aishwarya", 22);
        s1.display();
    }
}

5) Multiple Objects with Constructor

You can create multiple objects with different values.


class Car {

    String brand;
    String color;

    Car(String b, String c) {
        brand = b;
        color = c;
    }

    void show() {
        System.out.println("Brand: " + brand);
        System.out.println("Color: " + color);
    }
}

public class CarConstructorExample {
    public static void main(String[] args) {

        Car c1 = new Car("BMW", "Black");
        Car c2 = new Car("Tata", "White");

        c1.show();
        System.out.println("-----");
        c2.show();
    }
}

6) Constructor Overloading

Constructor overloading means: multiple constructors in the same class with different parameters.


class Demo {

    Demo() {
        System.out.println("Default Constructor");
    }

    Demo(int x) {
        System.out.println("Parameterized Constructor: " + x);
    }
}

public class ConstructorOverloadingExample {
    public static void main(String[] args) {

        Demo d1 = new Demo();
        Demo d2 = new Demo(100);
    }
}

Conclusion

Constructors are important in Java because they help initialize objects easily.

Now you are ready to learn the next important OOP concept: Encapsulation.

In the next post, we will learn about Encapsulation in Java with real examples!

Comments

Post a Comment

Popular posts from this blog

Inheritance in Java (Single, Multilevel, Hierarchical)

The Java Blueprint: Your First Step into Software Development

Java Variables and Data Types: Storing Your Data