Inheritance Flashcards
What will the following code print when compiled and run?
… main
List list = new ArrayList<>();
StringBuilder sb = new StringBuilder("mrx"); String s = sb.toString(); list.add(s); System.out.println(s.getClass()); System.out.println(list.getClass());
class java.lang.String class java.util.ArrayList
The getClass method always returns the Class object for the actual object on which the method is called irrespective of the type of the reference. Since s refers to an object (and not the reference) of class String, s.getClass returns Class object for String and similarly list.getClass returns Class object for ArrayList.
Consider the following code: interface Flyer { String getName(); } class Bird implements Flyer { public String name; public Bird(String name) { this.name = name; } public String getName(){ return name; } } class Eagle extends Bird { public Eagle(String name){ super(name); } } public class TestClass { public static void main(String[] args) throws Exception { Flyer f = new Eagle("American Bald Eagle"); //PRINT NAME HERE } }
Which of the following lines of code will print the name of the Eagle object?
System.out.println(f.name); System.out.println(f.getName()); System.out.println(((Eagle)f).name); System.out.println(((Bird)f).getName()); System.out.println(Eagle.name); System.out.println(Eagle.getName(f));
While accessing a method or variable, the compiler will only allow you to access a method or variable that is visible through the class of the reference.
When you try to use f.name, the class of the reference f is Flyer and Flyer has no field named “name”, thus, it will not compile.
But when you cast f to Bird (or Eagle), the compiler sees that the class Bird (or Eagle, because Eagle inherits from Bird) does have a field named “name” so ((Eagle)f).name or ((Bird)f).name will work fine.
f.getName() will work because Flyer does have a getName() method.
interface Flyer { String getName(); } class Bird implements Flyer { public String name; public Bird(String name) { this.name = name; } public String getName(){ return name; } } class Eagle extends Bird { public Eagle(String name){ super(name); } } public class TestClass { public static void main(String[] args) throws Exception { Flyer f = new Eagle("American Bald Eagle"); //PRINT NAME HERE } }