博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

The java.lang.Object Class

Posted on 2005-06-06 21:03  dalongzero  阅读(340)  评论(0)    收藏  举报
The Java language API includes a special class named Object that is the root
class of the entire Java hierarchy. The Object class, found in the java.lang package,
is the parent of every Java class, either directly (meaning the class is an
immediate child of Object) or indirectly (meaning Object is an ancestor further
up the inheritance tree).
For example, consider the following Employee class that does not declare a
parent class.
public class Employee
{
//Class definition
}

Because this Employee does not explicitly extend another class, it implicitly
extends Object. In fact, we could have added the extends keyword as follows:
public class Employee extends Object
{
//Class definition
}
If you write a class and do not explicitly extend another class, the
compiler adds “extends Object” to your class declaration. If you write a
class that extends another class besides Object, the class still is a child of
Object, since eventually one of its ancestors must have extended Object.
Suppose that a Salary class is written that extends Employee:
public class Salary extends Employee
{
//Class definition
}
The Salary class extends Employee, and because a Java class can only have
one parent class, Salary does not extend Object directly. Because Employee
extends Object, however, the Salary class is an indirect child of the Object class.
Note that the compiler does not add “extends Object” to a child class that
already extends another class.