Sie sind auf Seite 1von 4

Constructors in java Constructors are used to assign initial values to instance variables of the class.

A java constructor has the same name as the name of the class to which it belongs. Constructor is always called by new operator. Constructors are declared just like as we declare methods, except that the constructors dont have any return type. Constructors can be overloaded provided they should have different arguments because JVM differentiates constructor on the basis of arguments passed in the constructor.

Constructor Overloading Constructor overloading is a technique in which a class can have any number of constructors that differ in parameter lists. The compiler differentiates these constructors by taking into account the number of parameters in the list and their type. This example shows that the Cube1 constructor is overloaded one being the default constructor and the other being a parameterized constructor. public class Cube1 { int length; int breadth; int height; public int getVolume() { return (length * breadth * height); } Cube1() { length = 10; breadth = 10; height = 10; } Cube1(int l, int b, int h) { length = l; breadth = b; height = h;

} public static void main(String[] args) { Cube1 cubeObj1, cubeObj2; cubeObj1 = new Cube1(); cubeObj2 = new Cube1(10, 20, 30); System.out.println("Volume of Cube1 is : " + cubeObj1.getVolume()); System.out.println("Volume of Cube1 is : " + cubeObj2.getVolume()); } }

Constructor Chaining Consider a scenario where a base class is extended by a child. Whenever an object of the class child is created, the constructor of the parent class is invoked first. This is called Constructor chaining. Using the Keyword super Accessing Superclass Members If our method overrides one of its superclass' methods, you can invoke the overridden method through the use of the keyword super. We can also use super to refer to a hidden field. Subclass Constructors This example illustrates how to use the super keyword to invoke a superclass's constructor. MountainBike is a subclass of Bicycle. Here the MountainBike (subclass) constructor calls the superclass constructor and then adds initialization code of its own: public MountainBike(int startHeight, int startCadence,

int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } Invocation of a superclass constructor must be the first line in the subclass constructor. The syntax for calling a superclass constructor is super(); --or-super(parameter list);

With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called. If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem. If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent. Every constructor calls its superclass constructor. An implied super() is therefore included in each constructor which does not include either the this() function or an explicit super() call as its first statement. The super() statement invokes a constructor of the super class.

The implicit super() can be replaced by an explicit super(). The super statement must be the first statement of the constructor. The explicit super allows parameter values to be passed to the constructor of its superclass and must have matching parameter types A super() call in the constructor of a subclass will result in the call of the relevant constructor from the superclass, based on the signature of the call. This is called constructor chaining.

Das könnte Ihnen auch gefallen