[HD Pg 374, Sec. 11.6.0 - exercises]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
jjoseorione
Posts: 3
Joined: Tue Oct 11, 2022 1:47 pm
Contact:

[HD Pg 374, Sec. 11.6.0 - exercises]

Post by jjoseorione »

Hello, my English is not very good, so I hope I can explain my question. This question is about the OCA Java Programmer 8 Book. Section 11.6 Exercises. The problem 1 says:
1. Given the following two classes:

Code: 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
}
The above code refers to a class named PdfDocument. Write code for this class such that TestClass will print "pdf" when executed
My first answer (and maybe the most common wrong answer) was the following:

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;
}
But the output to the previous code was
dummy
Then, I rewrite the PdfDocument class:

Code: Select all

class PdfDocument extends Document{
	public PdfDocument(){
		super.setType("pdf");
	}
}
The output to this was
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.

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 11.6.0 - exercises]

Post by admin »

There are two points that you need to remember:
Point 1:
Indeed, the actual object is of type PdfDocument and PdfDocument inherits the getType() method. However, the getType() method that PdfDocument inherits has no knowledge of the fields and their values defined in the PdfDocument class. The getType() method was written in Document class and it knows only about the fields present in the Document class. What if your PdfDocument class doesn't define a type field at all? How will the developer of Document class know what fields it's subclass will have in future? They can't predict, right?

Point 2:
Variable/fields are not polymorphic (only methods are polymorphic). Which variable is accessed depends on the declared type of the reference through which it is accessed (not on the type of the object to which the reference points). In Document class's getType() method, we have "return type;", which is really a shortcut for "return this.type;" and the declared type for "this" is Document (not PdfDocument), so, it will return Document's type and not PdfDocument's type.

Therefore, when the inherited getType executes, it returns "dummy".

Yes, your second approach is correct. That is how it should be done in the given situation.

Ideally, the type field in Document should be made protected (because we expect subclasses to inherit it) and then PdfClass's constructor should be:

Code: Select all

	public PdfDocument(){
		type = "pdf";
	}      
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 220 guests