Hello, I am working on a study guide in preparation for a test on Java and there
ID: 3594035 • Letter: H
Question
Hello,
I am working on a study guide in preparation for a test on Java and there are some questions that I'm not sure about. I will post pictures of them to show what they are about.
Thanks!
Explanation / Answer
I) 15 points
1.) Point out and fix the errors in the Class "Novel"
Ans.) print function in Novel class tries to print the "name" and "author" from the parent class that is Book.
But in Book class the variables name and author are private variables.
One of the properties that private variables have is, they can access by public methods of that class only
So, if we want to access private variables name and author then we should access them by using public methods of Book class that is getName and getAuthor methods
Finally the changes should required in Novel class are
System.out.println("Novel ("+genre+"):"+getName()+"by"+getAuthor());
2.) Point out and fix the errors in the class Texbook
Ans.) Errors:
i) In constructor of Textbook class they try to assign string name to name variable.
But name variable has private access in class Book.
So we can't access the variable directly, we can access by using public methods of that class that is Book
setName(name);//instead of this.name = name;
ii) In constructor of Textbook class they try to assign string author to author variable.
But author variable has private access in class Book.
So we can't access the variable directly, we can access by using public methods of that class that is Book.
setAuthor(author);//instead of this.author = author;
iii) In print method of Textbook class they try to access name and author but they have private access in Book class.
So we don't access them directly but we can by using public methods of that class that is Book class.
System.out.println(subject+":"+getName()+"by"+getAuthor());// instead of System.out.println(subject + ":" + name + "by" + author);
II) 25 points
1.) Explain why the statements myBook1.print() and myBook2.print() make errors in the main.
Then, show how to fix these errors. DO NOT modify the main method of class TestClass but you could modify other classes
Ans.) myBook1.print() and myBook2.print() methods are getting the error that is "can not find the symbol".
why : If a parent reference variable is holding the reference of the child class.
The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class.
It is because compiler uses special run-time polymorphism mechanism only for methods.
How to avoid : We can access the child class method using parent reference which holds the object of child class.
By typecasting the child class object to parent reference we can actually achieve it but in question says that we could not change the code in main method but
you can change the other classes.
Explanation : if we add an empty print() method in parent class that is Book class,
By doing this we can actually avoid errors in myBook1.print() and myBook2.print() methods.
add the following code in Book class
public void print()
{
}
Compiler assumes that it is calling the function exactly presented in the child class.
So there are no errors.
2.) Is there any error resulted by the statement myBook3 = myBook1? If there is, explain the error and show how to fix it.
Ans.) Yes, there is a compile time error called "incompatible types"
why : myBook3 is a type of Novel class but myBook1 is a type of Book class reference variable holding Novel Class object.
Even myBook1 is Novel class object but it refers by Book class.
So you can't assign Book class reference to Novel class reference.
How to avoid : If you want to avoid, there is only one way that is you should typecast the myBook1 reference variable of type Book class to Novel class.
that is "myBook3 = (Novel)myBook1;" // instead of myBook3 = myBook1;
3.) Is there any error resulted by the statement myBook3 = (Novel)myBook2?
Ans.) Yes, there is a run time exception called "ClassCastException : Textbook can not be cast to Novel".
why : myBook3 = (Novel)myBook2;
in the above statement we try to assign myBook2 reference variable which is a type of Textbook class to myBook3 reference variable which is a type of Novel class.
but we try to cast the myBook2 reference variable which is a type of Textbook class to Novel class.
It is an illegal type casting.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.