Java Reference
In-Depth Information
public class T3Child extends T3 {
public T3Child() {
super(); // Ok. Calls T3() constructor, which is declared protected.
}
}
The T3 class is called the parent class of the T3Child class. An object of a child class cannot be created until the
object of its parent class is created. Note the use of the super() statement inside T3Child() constructor's body. The
statement super() calls the protected constructor of the T3 class. I use the super keyword to call the parent class
constructor as you use keyword this to call another constructor of the same class. You cannot call the protected
constructor of T3 directly as
new T3() ;
outside the com.jdojo.cls.p1 package.
Consider a T4 class with a constructor having package-level access. Recall that using no access level modifier
gives package-level access.
// T4.java
package com.jdojo.cls.p1;
public class T4 {
// T4() has package-level access
T4() {
}
}
You can use T4 's constructor to create its object anywhere in the com.jdojo.cls.p1 package. Sometimes you
need a class that works as a helper class for other classes in a package. Objects of these classes need to be created only
within the package. You can specify package-level access for constructors of such helper classes.
Default Constructor
The primary goal of declaring a class is to create an object of its type. You need a constructor to create an object
of a class. The necessity to have a constructor for a class is so obvious that the Java compiler adds a constructor to
your class if you do not declare one. The constructor that is added by the compiler is called the default constructor.
The default constructor does not have any parameters. Sometimes the default constructor is also called a no-args
constructor. The access level of the default constructor is the same as the access level of the class.
The classes that you have been working with are called top-level classes. You can also declare a class within
another class, which is called an inner class. A top-level class can have public or package-level access. However,
an inner class can have public , private , protected , or package-level access. The Java compiler adds a default
constructor for a top-level class as well as for a nested class. A default constructor for a top-level class can have either
public or package-level access depending on the access level of the class. However, a default constructor for an inner
class can have access level of public , private , protected or package-level depending on its class access level.
Table 6-6 shows some examples of classes and the compiler adding a default constructor to them. When the
compiler adds a default constructor, it also adds a statement super() to call the no-args constructor of the parent
class. Sometimes the call to the parent's no-args constructor inside the default constructor may cause your class not to
compile. Please refer to the chapter on inheritance for a complete discussion on this topic.
 
Search WWH ::




Custom Search