+ 1
What is constructor...any help??
2 Antworten
+ 19
Constructors
Constructors are special methods invoked when an object is created and are used to initialize them.
A constructor can be used to provide initial values for object attributes.
- A constructor name must be same as its class name.
- A constructor must have no explicit return type.
//Example of a constructor :::
public class Vehicle {
private String color;
Vehicle() {
color = "Red";
}
}
/*
The Vehicle() method is the constructor of our class, so whenever an object of that class is created, the color attribute will be set to "Red".
A constructor can also take parameters to initialize attributes.
*/
+ 1
😊👍