Java
Java
Constructors in Java
a constructor is a special method used to initialize objects. It is called automatically when an object of a class is
created. Constructors are essential for setting up the initial state of an object or assigning default values to its
attributes.
Key Features of Constructors
Same Name as the Class: A constructor must have the same name as the class it belongs to.
No Return Type: Constructors do not have a return type, not even void.
Called Automatically: They are invoked automatically when an object is created using the new keyword.
Types of Constructors
Default Constructor:
A constructor with no parameters.
If no constructor is explicitly defined, Java provides a default constructor.
Parameterized Constructor:
A constructor that accepts parameters to initialize the object with specific values.
No-Argument Constructor:
A constructor explicitly defined without parameters (similar to the default constructor but written by the
programmer).
Chaining: You can call one constructor from another using the this() keyword.
Inheritance: Constructors are not inherited, but a subclass can call a superclass constructor using super().
Flow of control
Java executes statements sequentially, one after another
Flow control statements alter this sequence
Alternation statements: if, if-else,if-else if-else,nested if switch
Looping statements: for, while, do-while
Escape statements: break, continue, return
Arrys
* Arrays store lists of similar items
* Arrays have fixed properties: name, type, and length
* These properties must be declared when creating the array
* Array sizes cannot be changed during program execution