Constructors
Let's start with some basic scenarios.
Default Constructor: Create a class named
Personwith 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
Personclass, which initializes thenameattribute to "Unknown" andageattribute to 0.Parameterized Constructor: Define a class named
Bookwith 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
Bookclass.Multiple Constructors: Implement a class
Rectanglewith 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.
Initialization Using Constructors: Create a class
Circlewith 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 theCircleclass.Constructor Overloading: Extend the
Circleclass 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
Circleclass.Initialization with Static Block: Create a class
MathConstantswith 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
PIandEconstants are initialized using a static block.Initialization Using Instance Initialization Block: Create a class
Dogwith 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
nameandbreedfields of theDogclass are initialized using an instance initialization block.Copy Constructor: Implement a copy constructor in a class
Carthat 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
Carclass has a copy constructor that initializes its fields with the values from anotherCarobject.Initialization with Final Fields: Implement a class
ImmutablePointwith 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
ImmutablePointclass has final fieldsxandy, which are initialized using a constructor.Initialization with Dependency Injection: Design a class
CircleCalculatorwith a dependency on theCircleclass 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
CircleCalculatorclass has a dependency on theCircleclass, 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
Was this helpful?