Understand reference type tests, Object methods, class inheritance, and subtype polymorphism in Java SE 25.
Answer: For a permitted reference type test, value instanceof Type is true when value refers to a non-null object that can be treated as Type at runtime. This includes an instance of the named class, a subclass, an implementation of the named interface, or a compatible array. Testing null returns false.
For example, if Object value = "Java";, both value instanceof String and value instanceof CharSequence are true. Pattern matching can also introduce a variable: if (value instanceof String text) { System.out.println(text.length()); }. Inside that branch, text is a non-null String. The compiler still checks whether the test is legal; instanceof does not make unrelated or non-reifiable type tests universally valid.
Answer: Yes. Every class except java.lang.Object has Object as a direct or indirect superclass. Object itself has no superclass. An ordinary class declaration without an explicit superclass normally extends Object directly.
Some class forms have a prescribed superclass: a record extends java.lang.Record, and an enum extends java.lang.Enum. Both ultimately lead to Object. Interfaces do not extend Object, although references with interface types can refer to objects and participate in the language's Object-method rules. Arrays are also objects, with Object as their superclass and Cloneable and Serializable as implemented interfaces.
Answer: No. Inheritance follows the usual access and overriding rules, and Object's methods serve different purposes. Classes commonly override toString(), equals(Object), and hashCode(). Equal objects must have equal hash codes, so equality and hashing must be designed together. Final methods such as getClass() cannot be overridden.
clone() is protected in Object; it does not provide a public copying operation on every object. Object's cloning mechanism also depends on Cloneable. finalize() is deprecated for removal and must not be used as a dependable cleanup mechanism. Use explicit resource management, such as try-with-resources for AutoCloseable resources, instead of relying on garbage collection to release resources promptly.
Answer: Inheritance expresses a subtype relationship and can provide shared behavior. Code written against a superclass or interface can work with multiple implementations while invoking their overridden instance methods through a common contract.
Reuse alone is not sufficient justification for extending a class. A subclass should be usable where the parent type is expected without violating that type's behavioral contract. When one object merely needs another object's services, composition and delegation often express the relationship more clearly. Choose inheritance for a suitable type relationship, not simply to avoid copying a few methods.
Answer: It describes a subtype relationship established by the type system. With class Car extends Vehicle, a Car is a Vehicle. With class Report implements Printable, a Report is a Printable. An interface can also extend another interface.
The relationship permits assignments such as Vehicle vehicle = new Car(); when the classes and constructor are accessible. Two classes do not acquire a subtype relationship merely because they contain similarly named fields or methods. The declared relationship and applicable Java type rules matter.
Answer: In a suitable domain model, class Mustang extends Horse and class GrizzlyBear extends Bear express that each specialized animal is also an instance of the broader type. The parent class's promised behavior must remain valid for the subclass.
Compare this with a car containing an engine: a Car has an Engine, so an engine field is usually appropriate. Making Car extend Engine would express the wrong type relationship even if it made some implementation code convenient to reuse.
Answer: It makes Vehicle the direct superclass of Car and makes Car a subtype of Vehicle. Car can inherit accessible members and override eligible instance methods. Constructors are not inherited.
A Car reference can be widened to Vehicle without a cast. The reverse is not automatically safe: a Vehicle reference might refer to a different subclass. Use a suitable type test or a design based on the Vehicle contract when specialized behavior is needed. Car has one direct superclass, although it can implement multiple interfaces.
Answer: Declare Mammal as an Animal subclass and override an eligible instance method with a compatible declaration. The reference's declared type determines which operations are available at compile time; the actual object's class determines the overridden implementation invoked at runtime.
public class AnimalTypes {
static class Animal {
String kind() { return "Animal"; }
}
static class Mammal extends Animal {
@Override
String kind() { return "Mammal"; }
}
public static void main(String[] args) {
Animal animal = new Mammal();
System.out.println(animal.kind());
System.out.println(animal instanceof Mammal);
Object absent = null;
System.out.println(absent instanceof Animal);
}
}
The output is Mammal, true, and false, on separate lines. Assigning the Mammal reference to an Animal variable does not transform the object into a different class.
Answer: No. Counting successful type tests is not a useful definition of polymorphism. Subtype polymorphism lets code use values of different concrete types through a common superclass or interface. Overridden instance methods can then provide behavior appropriate to each actual object.
In the preceding example, the Animal variable refers to a Mammal and the call invokes Mammal's implementation. A subclass remains a subtype even if it adds no overrides. Java also supports other forms of polymorphism, including generic code parameterized by types; the concept is broader than an instanceof checklist.
References: JLS: types and subtyping, JLS: classes and inheritance, JLS: expressions and method invocation, and Object API.