Constructors
Let's start with some basic scenarios.
Default Constructor: Create a class named
Person
with a default constructor that initializes the name to "Unknown" and age to 0.Explanation: In this example, a default constructor is defined for the
Person
class, which initializes thename
attribute to "Unknown" andage
attribute to 0.Parameterized Constructor: Define a class named
Book
with a parameterized constructor that takes title and author as parameters and initializes them.Explanation: This constructor allows the user to specify the title and author of the book when creating an instance of the
Book
class.Multiple Constructors: Implement a class
Rectangle
with two constructors: one that takes length and width as parameters and another that initializes both to 0.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
Circle
with a constructor that takes the radius as a parameter and initializes the radius of the circle.Explanation: The constructor
Circle(double radius)
allows the user to initialize the radius of the circle when creating an instance of theCircle
class.Constructor Overloading: Extend the
Circle
class to include overloaded constructors that take different types of parameters (e.g., int, float, double).Explanation: This example demonstrates constructor overloading, where multiple constructors with different parameter types are defined in the
Circle
class.Initialization with Static Block: Create a class
MathConstants
with constant values (e.g., PI, E) initialized using a static block.Explanation: Static blocks are used to initialize static variables in a class. In this example, the
PI
andE
constants are initialized using a static block.Initialization Using Instance Initialization Block: Create a class
Dog
with instance fields (e.g., name, breed) initialized using an instance initialization block.Explanation: Instance initialization blocks are used to initialize instance variables in a class. In this example, the
name
andbreed
fields of theDog
class are initialized using an instance initialization block.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.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 anotherCar
object.Initialization with Final Fields: Implement a class
ImmutablePoint
with final fields representing x and y coordinates and initialize them using a constructor.Explanation: Final fields cannot be modified once initialized. In this example, the
ImmutablePoint
class has final fieldsx
andy
, which are initialized using a constructor.Initialization with Dependency Injection: Design a class
CircleCalculator
with a dependency on theCircle
class and initialize it using constructor injection.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 theCircle
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