if no superclass is specified when a class is define
时间: 2024-03-23 14:35:50 浏览: 63
If no superclass is specified when a class is defined in Java, it implicitly becomes a subclass of the `Object` class. This means that every class in Java is either a direct or an indirect subclass of the `Object` class, which is the root of the class hierarchy.
The `Object` class provides some common methods that are inherited by all classes, such as `equals()`, `hashCode()`, and `toString()`. For example, if a class does not explicitly override the `toString()` method, it will inherit the implementation of the `toString()` method from the `Object` class, which returns a string representation of the object's memory address.
Here is an example of a class that does not specify a superclass:
```java
public class MyClass {
// class code
}
```
In this example, `MyClass` does not extend any other class, so it implicitly extends the `Object` class. Therefore, it inherits the methods of the `Object` class and can be treated as an object in Java.
阅读全文