Java

Java - Constructors

Java - Constructors

Constructors are special methods used in Java to create objects. They can take parameters that can be used to initialize the object’s state, or they can have no arguments at all. Constructors are usually declared with the same name as the class and do not return any value—neither explicitly nor implicitly. 

When an object is instantiated, its constructor is called automatically by the system unless a user-defined one has overridden it. This ensures that all class members have been initialized properly before being accessed from within other parts of the code, thus providing robustness and reliability for applications using this language. 

In addition to initializing variables, constructors also provide a way for developers to implement logic specific to their classes without having to write extra public methods that would clutter up the interface of their applications. Moreover, when creating subclasses from existing constructors allow developers to define how exactly new objects should be created - giving them control over what values will get passed down during inheritance and ensuring each inherited instance behaves according to its own unique requirements if needed! It is clear why constructors play such an important role in Java development today - allowing users flexibility while still maintaining strict standards on how data should be handled inside their programs! 

Creating a constructor: 
To create a constructor in a class, you use the same name as the class and do not use any return type. 

class Car { 
int speed; 
String colour; 
// constructor 
Car() { 
speed = 0; 
colour = "white"; 
} 
} 


This constructor initializes the speed and color fields of the Car class with default values when a new object of the Car class is created. 

Constructor Overloading 
A class can have more than one constructor, each with a different set of parameters. This is called constructor overloading. This feature allows you to create objects of the class with different initial values. 

class Car { 
int speed; 
String color; 
String make; 
// default constructor 
Car() { 
speed = 0; 
color = "white"; 
make = "unknown"; 
} 
// constructor overloading with only speed parameter 
Car(int speed) { 
this.speed = speed; 
color = "white"; 
make = "unknown"; 
} 
// constructor overloading with only color parameter 
Car(String color) { 
speed = 0; 
this.color = color; 
make = "unknown"; 
} 
// constructor overloading with both speed and color parameter 
Car(int speed, String color) { 
this.speed = speed; 
this.color = color; 
make = "unknown"; 
} 
// constructor overloading with speed, color and make parameter 
Car(int speed, String color, String make) { 
this.speed = speed; 
this.color = color; 
this.make = make; 
} 
} 


In this example, the class "Car" has four constructors. Each constructor has a different set of parameters. The first one is the default constructor with no parameters present. The second constructor accepts only one parameter, "speed", the third constructor accepts only one parameter, "color", the fourth constructor accepts two parameters, "speed" and "color" and the last one accepts three parameters, "speed", "color" and "make". 

The objects for the class car can be created in the following ways: Car car1 = new Car(); 

Car car2 = new Car(90); 
Car car3 = new Car("red"); 
Car car4 = new Car(120, "black"); 
Car car5 = new Car(120, "black", "BMW"); 

Each of the above objects will be initialized with the respective constructor parameters. 

Top course recommendations for you

    Docker Swarm
    1 hrs
    Beginner
    1.7K+ Learners
    4.52  (107)
    Factorial Program in C
    2 hrs
    Beginner
    4.6K+ Learners
    4.43  (155)
    Jenkins Tutorial
    1 hrs
    Beginner
    8.5K+ Learners
    4.5  (599)
    Dockerize Spring Boot Application
    1 hrs
    Intermediate
    3.9K+ Learners
    4.42  (297)
    Python Data Structures
    1 hrs
    Beginner
    30.9K+ Learners
    4.51  (2315)
    Fibonacci Series in Java
    2 hrs
    Beginner
    2.7K+ Learners
    4.33  (60)
    Priority Queue in C++
    1 hrs
    Beginner
    2.2K+ Learners
    4.31  (98)
    Introduction to MATLAB
    2 hrs
    Beginner
    22.1K+ Learners
    4.41  (1676)
    Packages in Python
    1 hrs
    Beginner
    7.2K+ Learners
    4.32  (542)
    Palindrome in Python
    2 hrs
    Beginner
    2.9K+ Learners
    4.64  (86)