Monday, May 16, 2011

Explicit call to super class constructor in java

Normally, you don't explicitly write the constructor for your parent class, but there are two cases where this is necessary:

 

Passing parameters

You want to call a parent constructor which has parameters (the default construct has no parameters). For example, if you are defining a subclass of JFrame you might do the following.

class Chairman extends Employee {
. . .
//======== constructor
public Chairman(String name_,String designation_) {
super(name_);
This was shown here.

No parameterless constructor in parent


There is no parent constructor with no parameters. Sometimes is doesn't make sense to create an object without supplying parameters.

No comments:

Post a Comment

Chitika