Домой United States USA — software Exploring the java.lang Library — Developer.com

Exploring the java.lang Library — Developer.com

136
0
ПОДЕЛИТЬСЯ

NewsHubThe java.lang library contains classes and interfaces that are fundamental to every Java program. This package is imported implicitly by the compiler into all programs, so we do not use the import statement to explicitly include it. Due to its tacit nature of inclusion, the significance of its impact often goes unnoticed. The most important class of this library is Object. Every class design in Java is actually a subclass of this class, either directly or indirectly. In other words, Object is the parent of all the classes in Java. Apart from this primordial class, the package contains numerous other classes and interfaces. The article takes on just two of them, the Object class and the collection of wrapper classes of this package, to get a glimpse of the key aspects of this library.
A Java programmer uses inheritance to create classes from existing classes. The primary motivation behind inheritance is to extend the quality of its parent class. That means, if a class has, say, two methods:
and another class, B, extends class A, and has a single method:
class B extends the quality of class A by inheriting the methods defined in class A, in addition to the methods defined within itself.
This is what happens with the Object class. The Object class methods are implicitly inherited by all Java classes. But, the difference is that we do not explicitly use the extend keyword; instead, it is implied by default. In this case, the responsibility of applying the inheritance hierarchy to Java classes rests with the compiler. However, there is no harm in doing so explicitly, although it is utterly unnecessary.
In relation to this, note that the Employee class never inherits the constructor of the Object class as per the object-oriented principle of superclass-subclass relationship. The Object constructor is implicitly called by the Employee constructor as soon as its objects are created. It is the first task of any subclass constructor to call its immediate parent class constructor, either explicitly or implicitly, to ensure that the instance variables inherited from the superclass are initialized properly before using them.
The Object class provides a number of methods that are universally applicable to all its subclasses. The most common of this is the toString() method. This method returns the string representation of an object. It is particularly useful for debugging purposes. It is recommended that every class overrides this method to get a customized string representation of the object. By default, the toString() method returns the package name with class name, concatenated by an ‘@’ and the hash code of the class instance. As a result, the following two operation invariably have the same string representation:
The hash code is deemed as a distinct integer that uniquely identifies an object.

Continue reading...