My first answer (and maybe the most common wrong answer) was the following:1. Given the following two classes:The above code refers to a class named PdfDocument. Write code for this class such that TestClass will print "pdf" when executedCode: Select all
public class TestClass{ public static void main(String[] args){ Document d = new PdfDocument(); System.out.println(d.getType()); //should print "pdf" } } class Document{ private String type = "dummy"; private byte[] data; //insert appropriate getters and setters }
Code: Select all
public class TestClass{
public static void main(String[] args){
Document d = new PdfDocument();
System.out.println(d.getType()); //debe imprimir "pdf"
}
}
class Document{
private String type = "dummy";
private byte[] data; //Insertar setters y getters apropiados
public String getType(){
return type;
}
public byte[] getData(){
return data;
}
public void setType(String type){
this.type = type;
}
public void setData(byte[] data){
this.data = data;
}
}
class PdfDocument extends Document{
private String type = "pdf";
private byte[] data;
}
Then, I rewrite the PdfDocument class:dummy
Code: Select all
class PdfDocument extends Document{
public PdfDocument(){
super.setType("pdf");
}
}
I don't really understand why the first answer was wrong. I really thought that the d.getType() at TestClass's main would return "pdf" because the object created with the new keyword is of type PdfDocument() and it would execute the getType() method innherited to PdfDocument() and thus return the PdfDocument's type field. I'll thank you if you explain to me why this is wrong, also if you tell me if my second answer was the expected.