The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Java
What does a class inherit through extends?
Inheritance is syntax in which you write extends and another class after the class name, as in class Dog extends Animal. The Animal after extends is the superclass, Dog is the subclass, and Dog inherits the methods and fields written in Animal. So you can call eat(), which is not written in Dog, on a Dog object. If you write an inherited method again in Dog with the same name and the same parameters, the one in Dog runs on a Dog object, and this is overriding. And because you used extends, you can put a Dog object in a variable of type Animal. In this lab you remove extends, write eat() again, and change the type of the variable, and the same code is compiled and run each time. You see what the compiler reports and which eat runs.
Checked against real execution · Eight combinations were compiled with javax.tools and run on JDK 21, and the results were checked against the model
class Animal {
void eat() {
System.out.println("eats");
}
}
// Inheritance: Dog inherits what is written in Animal
class Dog extends Animal {
void bark() {
System.out.println("woof");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
The compiler and runtime are JDK 21.0.12.1 (javac 21). For all eight combinations, the same source as the code on the screen (identical line by line) was compiled with javax.tools. The ones that compiled had their class files loaded by a new class loader and Main.main was run, and the results were checked against the standard output that was captured. What was checked is whether compilation succeeded, the diagnostic code of the first error (compiler.err.cant.resolve.location.args and compiler.err.prob.found.req, shown on the screen as the two error descriptions), the number of errors and the lines where they occurred, whether the Main class file was created, and the output when run. The "eat that ran" was decided not by guessing but by the output. The eat of Animal was written to print "eats" and the eat of Dog to print "the dog eats", and the actual output was read to see which one it was. The facts that the step sentences rely on were also checked by separate runs. Calling bark(), which is written only in Dog, through an Animal-type variable is a compile-time error (so the code for the Animal variable has no d.bark() line). Even if eat is written again in Dog, the eat called on an Animal object still prints "eats". Even without extends, the bark written directly in Dog can be used without a problem. The documentation checked was the Java Language Specification (Java SE 21), opened directly. The rule that the extends clause names the direct superclass and that the class is called a subclass follows section 8.1.4. The rule that a class inherits the accessible methods of its superclass that it does not write again with the same signature follows section 8.4.8. The rule that a subclass writing a method of its superclass again with the same signature is overriding follows section 8.4.8.1. The rule that a superclass is a supertype of its subclass (so a subclass object can be put in a superclass-type variable) follows section 4.10.2. The rule that at run time the overriding method is looked up by starting from the actual class of the target object and going upward, not from the type of the variable, follows section 15.12.4.4. Sections 5.1.5 and 5.2 of the specification (the conversion that puts a value of a subclass type in a superclass-type variable in an assignment context) were not opened and checked in this lab. The kind of error, the number of errors, and the line where each occurs are not set by the specification. They were observed with this javac. When a Dog was put in an Animal variable without extends, the same one error occurred on the same line whether or not eat was written again in Dog. The code on the screen has the classes together in one file and shortens the access modifiers, and it does not use @Override. Every class inherits from Object even if it does not use extends, but this lab did not cover that, and the statement that without extends Dog has only what it writes itself is about what is written in Animal. The rule that a class also inherits the accessible fields of its superclass was checked in section 8.3, but this lab showed only methods by running them. Writing a field of the superclass, constructors, the super keyword, the fact that private members are not inherited, and the order in which a subclass calls a superclass constructor were also not covered in this lab. The order in which superclass constructors run and super are covered in "In-depth lab: Inheritance and overriding".
Previous lab: static · Next lab: Interfaces
Struck-through text was removed from the first version of the code; highlighted text was added.