Consider the following class declarations.public class Publication{private String title; public Publication(){title = "Generic";} public Publication(String t){title = t;}} public class Book extends Publication{public Book(){super();}public Book(String t){super(t);}}The following code segment appears in a method in another class.Book myBook = new Book("Adventure Story"); // Line 1Book yourBook = new Book(); // Line 2Which of the following best describes the result of executing the code segment?ResponsesThe variable myBook is assigned a reference to a new Book object created using the one-argument Book constructor, which uses super to set myBook’s title attribute to "Adventure Story". The variable yourBook is assigned a reference to a new Book object created using the no-argument Book constructor, which uses super to set yourBook’s title attribute to an empty string.The variable myBook is assigned a reference to a new Book object created using the one-argument Book constructor, which uses super to set myBook ’s title attribute to "Adventure Story" . The variable yourBook is assigned a reference to a new Book object created using the no-argument Book constructor, which uses super to set yourBook ’s title attribute to an empty string.The variable myBook is assigned a reference to a new Book object created using the no-argument Book constructor, which uses super to set myBook’s title attribute to "Generic". The variable yourBook is assigned a reference to a new Book object created using super to call to the Publication no-argument constructor to set yourBook’s title attribute to "Generic".The variable myBook is assigned a reference to a new Book object created using the no-argument Book constructor, which uses super to set myBook ’s title attribute to "Generic" . The variable yourBook is assigned a reference to a new Book object created using super to call to the Publication no-argument constructor to set yourBook ’s title attribute to "Generic" .The variable myBook is assigned a reference to a new Book object created using the one-argument Book constructor, which uses super to set myBook’s title attribute to "Adventure Story". The variable yourBook is assigned a reference to a new Book object created using super to call to the Publication no-argument constructor to set yourBook’s title attribute to "Generic".The variable myBook is assigned a reference to a new Book object created using the one-argument Book constructor, which uses super to set myBook ’s title attribute to "Adventure Story" . The variable yourBook is assigned a reference to a new Book object created using super to call to the Publication no-argument constructor to set yourBook ’s title attribute to "Generic" .A runtime error occurs in line 1 because the one-argument Publication constructor cannot be called from the one-argument Book constructor.A runtime error occurs in line 1 because the one-argument Publication constructor cannot be called from the one-argument Book constructor.A runtime error occurs in line 2 because the no-argument Publication constructor cannot be called from the no-argument Book constructor.
Question
Consider the following class declarations.public class Publication{private String title; public Publication(){title = "Generic";} public Publication(String t){title = t;}} public class Book extends Publication{public Book(){super();}public Book(String t){super(t);}}The following code segment appears in a method in another class.Book myBook = new Book("Adventure Story"); // Line 1Book yourBook = new Book(); // Line 2Which of the following best describes the result of executing the code segment?ResponsesThe variable myBook is assigned a reference to a new Book object created using the one-argument Book constructor, which uses super to set myBook’s title attribute to "Adventure Story". The variable yourBook is assigned a reference to a new Book object created using the no-argument Book constructor, which uses super to set yourBook’s title attribute to an empty string.The variable myBook is assigned a reference to a new Book object created using the one-argument Book constructor, which uses super to set myBook ’s title attribute to "Adventure Story" . The variable yourBook is assigned a reference to a new Book object created using the no-argument Book constructor, which uses super to set yourBook ’s title attribute to an empty string.The variable myBook is assigned a reference to a new Book object created using the no-argument Book constructor, which uses super to set myBook’s title attribute to "Generic". The variable yourBook is assigned a reference to a new Book object created using super to call to the Publication no-argument constructor to set yourBook’s title attribute to "Generic".The variable myBook is assigned a reference to a new Book object created using the no-argument Book constructor, which uses super to set myBook ’s title attribute to "Generic" . The variable yourBook is assigned a reference to a new Book object created using super to call to the Publication no-argument constructor to set yourBook ’s title attribute to "Generic" .The variable myBook is assigned a reference to a new Book object created using the one-argument Book constructor, which uses super to set myBook’s title attribute to "Adventure Story". The variable yourBook is assigned a reference to a new Book object created using super to call to the Publication no-argument constructor to set yourBook’s title attribute to "Generic".The variable myBook is assigned a reference to a new Book object created using the one-argument Book constructor, which uses super to set myBook ’s title attribute to "Adventure Story" . The variable yourBook is assigned a reference to a new Book object created using super to call to the Publication no-argument constructor to set yourBook ’s title attribute to "Generic" .A runtime error occurs in line 1 because the one-argument Publication constructor cannot be called from the one-argument Book constructor.A runtime error occurs in line 1 because the one-argument Publication constructor cannot be called from the one-argument Book constructor.A runtime error occurs in line 2 because the no-argument Publication constructor cannot be called from the no-argument Book constructor.
Solution
The correct answer is: "The variable myBook is assigned a reference to a new Book object created using the one-argument Book constructor, which uses super to set myBook’s title attribute to "Adventure Story". The variable yourBook is assigned a reference to a new Book object created using super to call to the Publication no-argument constructor to set yourBook’s title attribute to "Generic"."
Here's why:
-
On Line 1, a new Book object is created with the title "Adventure Story". This is done by calling the one-argument constructor of the Book class, which in turn calls the one-argument constructor of the Publication class (its superclass) using the super keyword. The title of the Publication is set to the string passed to it, which in this case is "Adventure Story". This new Book object is then assigned to the variable myBook.
-
On Line 2, a new Book object is created without a specific title. This is done by calling the no-argument constructor of the Book class, which in turn calls the no-argument constructor of the Publication class using the super keyword. Since no title is provided, the Publication's title is set to "Generic" by default. This new Book object is then assigned to the variable yourBook.
Similar Questions
Select the correct answerWhat is the process of defining a method in a subclass having same name & type signature as a method in its superclass?
Given:class FourWheeler { public FourWheeler () { System.out.print(1); } } class Car extends FourWheeler{ public Car() { System.out.print(2); } } class Audi extends Car{ public Audi() {System.out.print(3); } } public class Driver{ public static void main( String[] argv ) {new Audi(); } } What is the result when this code is executed?Select one:a.The code runs with no outputb.321c.123d.3
What will be the output of the following code?class A { void method() { System.out.println("Class A"); }}class B extends A { void method() { super.method(); System.out.println("Class B"); }}public class Main { public static void main(String[] args) { B obj = new B(); obj.method(); }}
What is the expected output?class Animal { Animal() { System.out.println("Animal"); }}class Wild extends Animal{ Wild() { System.out.println("Wild"); super(); }}public class Test { public static void main(String args[]) { Wild wild = new Wild(); }}
Which keyword is used to prevent a method from being overridden in the subclass?Question 4Answera.finalb.staticc.abstractd.private
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.