0% found this document useful (0 votes)
18 views

Constructors

Constructors are used to initialize objects and must match the class name without a return type. They can invoke other constructors using this() or super() and are not inherited. The compiler automatically provides a no-argument default constructor unless another constructor is defined, in which case the developer must explicitly call the superclass constructor.

Uploaded by

Pradeep Tiwari
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Constructors

Constructors are used to initialize objects and must match the class name without a return type. They can invoke other constructors using this() or super() and are not inherited. The compiler automatically provides a no-argument default constructor unless another constructor is defined, in which case the developer must explicitly call the superclass constructor.

Uploaded by

Pradeep Tiwari
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

1 CONSTRUCTORS

A constructor is used when creating an object from a class. The constructor name must match the name of the class and must not have a return type. They can be overloaded, but they are not inherited by subclasses. Invoking constructors A constructor can be invoked only from other constructors. To invoke a constructor in the same class, invoke the this() function with matching arguments. To invoke a constructor in the superclass, invoke the super() function with matching arguments. When a subclass object is created, all the superclass constructors are invoked in the order starting from the top of the hierarchy. Default constructors The compiler creates the default constructor if you have not provided any other constructor in the class. It does not take any arguments. The default constructor calls the no-argument constructor of the superclass. It has the same access modifier as the class. However, the compiler will not provide the default constructor if you have written at least one constructor in the class. For example, the following class has a constructor with two arguments defined in it. Here the compiler will give an error if we try to instantiate the class without passing any arguments because the default constructor is not available:

class Dot { int x, y; Dot(int x, int y) { this.x = x; this.y = y; } } If you invoke the default constructor of your class and the superclass does not have a constructor without any arguments, your code will not compile. The reason is that the subclass default constructor makes an implicit call to the no-argument constructor of its superclass. For instance:

class Dot { int x, y; Dot(int x, int y) { this.x = x; this.y = y; } } class MyDot extends Dot { }

2
class Test { public static void main(String args[]) { MyDot dot=new MyDot(); } }

You might also like