Constructors in java

Each class (whether sub or super) should encapsulate its own initialization, usually relating to setting the initial state of its instance variable.

A constructor for a superclass should deal with general initialization.

Each subclass can have its own constructor for specialised initialization but it must often invoke the behavior of the superclass constructor. It does this using the keyword super.

class MySubClass extends MySuperClass

{

Public MySubClass (sub-parameters)

{

super (super-parameters);

// other initialization

}

If super is called, i.e. the superclass constructor, then this must be the first statement in the constructor.

Usually some of the parameter passed to MySubClass will be initializer values for superclass instance variables, and these will simply be passed on to the superclass constructor as parameters. In other words
super-parameter will be some (of all) of sub-parameters.

Shown below are two constructors, one for the Publication class and one for Book. The Book constructor requires four parameters three of which are immediately passed on to the superclass constructor to initialize its instance variables.

public Publication (String pTitle, double pPrice, int pCopies)

{

title = pTitle;

// etc.

}

public Book (String pTitle, String pAuthor, double pPrice, int pCopies)

{

Super(pTitle, pPrice, pCopies);

author = pAuthor;

// etc.

}

Constructor Rules

Rules exist that govern the invocation of superconstructor.

If the superclass has a parameterless (or default) constructor this will be called automatically if no explicit call to super is made in the subclass constructor though an explicit call is still better style for reasons of clarity.

However if the superclass has no parameterclass constructor but does have a parameterized one, this must be called explicitly using super.

On the left above

it is legal, though bad practice, to have a subclass with no constructor because superclass has a parameterless constructor.

In the center

if the subclass constructor doesn’t call super, the parameterless superclass constructor will be called.

On the right

because superclass has no parameterless constructor, subclass must have a constructor and it must call super. This is simply because a (super) class with only a parameterized constructor can only be initialized by providing the required parameter(s).

In java we have private, protected, public or default constructor.

Types of constructor

There are two types constructor:

  • Default constructor.
  • Parameterized constructor.
Default constructor

If there is no any parameter then the constructor is called default constructor.

Syntax

< class_name>(){}

Example of default constructor

//Java Program to create and call a default constructor

class Car1{

//creating a default constructor

Car1(){System.out.println(“Car Runs”);}

//main method

public static void main(String args[]){

//calling a default constructor

Car1 b=new Car1();

}

}

Output

Car Runs

Parameterized constructor

Constructor with arguments (parameter) is called parameterized constructor.

It is used to provide different values of distinct objects.

Example of

class Employee{

int id;

String name;

//creating a parameterized constructor

Employee(int i,String n){

id = i;

name = n;

}

//method to display the values

void display(){System.out.println(id+” “+name);}

public static void main(String args[]){

//creating objects and passing values

Employee e1 = new Employee(101,”Ajay”);

Employee e2 = new Employee(102,”Abhay”);

//calling method to display the values of object

e1.display();

e2.display();

}

}

Output

101 Ajay

102 Abhay

Copyright 1999- Ducat Creative, All rights reserved.