public class Animal {
private String name;
public Animal(String n) { name = n; }
public String getName() { return name; }
}
public class Dog extends Animal {
private String breed;
public Dog(String n, String b) {
super(n);
breed = b;
}
}
// Usage:
Dog d = new Dog("Rex", "Lab");
System.out.print(d.getName());What is printed?
- A
Lab
- B
RexLab
- Ccheck_circle
Rex
- D
Compile error: Dog has no getName
Explanation
Dog inherits getName() from Animal. The super(n) call passes "Rex" to Animal's constructor, which assigns it to name. Calling d.getName() returns "Rex".