Constructors

Let's start with some basic scenarios.

  1. Default Constructor: Create a class named Person with a default constructor that initializes the name to "Unknown" and age to 0.

    public class Person {
        String name;
        int age;
    
        public Person() {
            name = "Unknown";
            age = 0;
        }
    }

    Explanation: In this example, a default constructor is defined for the Person class, which initializes the name attribute to "Unknown" and age attribute to 0.

  2. Parameterized Constructor: Define a class named Book with a parameterized constructor that takes title and author as parameters and initializes them.

    public class Book {
        String title;
        String author;
    
        public Book(String title, String author) {
            this.title = title;
            this.author = author;
        }
    }

    Explanation: This constructor allows the user to specify the title and author of the book when creating an instance of the Book class.

  3. Multiple Constructors: Implement a class Rectangle with two constructors: one that takes length and width as parameters and another that initializes both to 0.

    public class Rectangle {
        int length;
        int width;
    
        public Rectangle(int length, int width) {
            this.length = length;
            this.width = width;
        }
    
        public Rectangle() {
            this(0, 0); // Call the parameterized constructor with default values
        }
    }

    Explanation: This example demonstrates how to define multiple constructors in a class. One constructor initializes the length and width to user-specified values, while the other initializes both to 0.

  4. Initialization Using Constructors: Create a class Circle with a constructor that takes the radius as a parameter and initializes the radius of the circle.

    public class Circle {
        double radius;
    
        public Circle(double radius) {
            this.radius = radius;
        }
    }

    Explanation: The constructor Circle(double radius) allows the user to initialize the radius of the circle when creating an instance of the Circle class.

  5. Constructor Overloading: Extend the Circle class to include overloaded constructors that take different types of parameters (e.g., int, float, double).

    public class Circle {
        double radius;
    
        public Circle(double radius) {
            this.radius = radius;
        }
    
        public Circle(int radius) {
            this.radius = (double) radius;
        }
    
        public Circle(float radius) {
            this.radius = (double) radius;
        }
    }

    Explanation: This example demonstrates constructor overloading, where multiple constructors with different parameter types are defined in the Circle class.

  6. Initialization with Static Block: Create a class MathConstants with constant values (e.g., PI, E) initialized using a static block.

    public class MathConstants {
        public static final double PI;
        public static final double E;
    
        static {
            PI = 3.14159;
            E = 2.71828;
        }
    }

    Explanation: Static blocks are used to initialize static variables in a class. In this example, the PI and E constants are initialized using a static block.

  7. Initialization Using Instance Initialization Block: Create a class Dog with instance fields (e.g., name, breed) initialized using an instance initialization block.

    public class Dog {
        String name;
        String breed;
    
        {
            name = "Unknown";
            breed = "Unknown";
        }
    }

    Explanation: Instance initialization blocks are used to initialize instance variables in a class. In this example, the name and breed fields of the Dog class are initialized using an instance initialization block.

  8. Copy Constructor: Implement a copy constructor in a class Car that takes another object of the same class and initializes its fields with the values from the given object.

    public class Car {
        String make;
        String model;
        int year;
    
        public Car(Car otherCar) {
            this.make = otherCar.make;
            this.model = otherCar.model;
            this.year = otherCar.year;
        }
    }

    Explanation: Copy constructors are used to create a new object with the same state as an existing object of the same class. In this example, the Car class has a copy constructor that initializes its fields with the values from another Car object.

  9. Initialization with Final Fields: Implement a class ImmutablePoint with final fields representing x and y coordinates and initialize them using a constructor.

    public class ImmutablePoint {
        final int x;
        final int y;
    
        public ImmutablePoint(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    Explanation: Final fields cannot be modified once initialized. In this example, the ImmutablePoint class has final fields x and y, which are initialized using a constructor.

  10. Initialization with Dependency Injection: Design a class CircleCalculator with a dependency on the Circle class and initialize it using constructor injection.

    public class CircleCalculator {
        private final Circle circle;
    
        public CircleCalculator(Circle circle) {
            this.circle = circle;
        }
    }

    Explanation: Dependency injection is a design pattern where dependencies of a class are provided from the outside. In this example, the CircleCalculator class has a dependency on the Circle class, which is injected via its constructor.

These problem statements cover the basics of constructors in Java, including default constructors, parameterized constructors, constructor overloading, initialization blocks, and copy constructors.

Last updated