When you write a constructor for a class it still has default constructor that Java automatically provides?

All Java classes have special methods called constructors that are used to initialize a new object of that type. Constructors have the same name as the class--the name of the Rectangle class's constructor is Rectangle, the name of the Thread class's constructor is Thread, and so on. Java supports method name overloading, so that a class can have any number of constructors, all of which have the same name. Like other overloaded methods, constructors are differentiated from one another by the number or type of their arguments.

Consider the Rectangle class in the java.awt package, which provides several different constructors, all named Rectangle, but each with a different number of arguments, or different types of arguments from which the new Rectangle object will get its initial state. Here are the constructor signatures from the java.awt.Rectangle class: class:

public Rectangle[] public Rectangle[int width, int height] public Rectangle[int x, int y, int width, int height] public Rectangle[Dimension size] public Rectangle[Point location] public Rectangle[Point location, Dimension size]
The first Rectangle constructor initializes a new Rectangle to some reasonable default, the second constructor initializes the new Rectangle with the specified width and height, the third constructor initializes the new Rectangle at the specified position and with the specified width and height, and so on.

Typically, a constructor uses its arguments to initialize the new object's state. When creating an object, you should choose the constructor whose arguments best reflect how you want to initialize the new object.

Based on the number and type of the arguments that you pass into the constructor, the compiler can determine which constructor to use. Thus the compiler knows that when you write:

new Rectangle[0, 0, 100, 200];
it should use the constructor that requires four integer arguments, and when you write:
new Rectangle[myPointObj, myDimensionObj];
it should use the constructor that requires one Point object argument and one Dimension object argument.

When you are writing your own class, you don't have to provide constructors for it. The default constructor, the constructor that takes no arguments, is automatically provided by the runtime system for all classes. However, you will often want or need to provide constructors for your class.

You declare and implement a constructor just like you would any other method in your class. The name of the constructor must be the same as the name of the class and, if you provide more than one constructor, the arguments to each constructor must differ in number or in type from the others. You do not specify a return value for a constructor.

The constructor for the following subclass of Thread, which implements a thread that performs animation, sets up some default values such as the frame speed, the number of images, and loads the images themselves:

class AnimationThread extends Thread { int framesPerSecond; int numImages; Image[] images; AnimationThread[int fps, int num] { super["AnimationThread"]; this.framesPerSecond = fps; this.numImages = num; this.images = new Image[numImages]; for [int i = 0; i

Chủ Đề