The constructor gets called when we create an object of a class (i.e. new keyword followed by class name). For e.g.
Demo obj = new Demo();
(here Demo() is a default constructor of Demo class).
Default constructor: It is also known as no-arg constructor. Constructor with no arguments is known as default constructor.
Examples:
class Demo { public Demo() { System.out.println("This is a default constructor"); } }
Parameterized constructor: Constructor with argument list is known as parameterized constructor.
class Demo { public Demo(int num, String str) { System.out.println("This is a parameterized constructor"); } }
No comments:
Post a Comment