Knowee
Questions
Features
Study Tools

You have a class Container:package com.skillsoft.test;public class Container { public Object myValue = new Object(); public void printMyValue(){ System.out.println(myValue); }}You have child classes BooleanContainer and IntegerContainer:public class BooleanContainer extends Container { public void printMyValue(){ System.out.println(myValue); }}public class IntegerContainer extends Container{ public void printMyValue(){ System.out.println(myValue); }}What can be improved about the setup of this inheritance hierarchy?Instruction: Choose the option that best answers the question. Answer ChoicesThere should be only one printMyValue() function in ContainerThe classes aren’t logically related to each other so there should be no inheritanceYou should ideally create child classes that are protectedYou should ideally create child classes that are private

Question

You have a class Container:package com.skillsoft.test;public class Container { public Object myValue = new Object(); public void printMyValue(){ System.out.println(myValue); }}You have child classes BooleanContainer and IntegerContainer:public class BooleanContainer extends Container { public void printMyValue(){ System.out.println(myValue); }}public class IntegerContainer extends Container{ public void printMyValue(){ System.out.println(myValue); }}What can be improved about the setup of this inheritance hierarchy?Instruction: Choose the option that best answers the question. Answer ChoicesThere should be only one printMyValue() function in ContainerThe classes aren’t logically related to each other so there should be no inheritanceYou should ideally create child classes that are protectedYou should ideally create child classes that are private

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The classes aren’t logically related to each other so there should be no inheritance.

Inheritance should be used when there is a clear, logical relationship between classes. In this case, BooleanContainer and IntegerContainer don't seem to have a logical relationship with the Container class. They are just using the printMyValue() method of the Container class, which doesn't justify an inheritance relationship.

Moreover, the printMyValue() method in the child classes is just repeating the same code as in the parent class, which is not a good practice. If the purpose is to print the value of myValue, it can be done directly by calling the method from the Container class, without needing to override it in the child classes.

So, the inheritance hierarchy can

This problem has been solved

Similar Questions

class FourWheeler{    public FourWheeler()    {        System.out.println("Class FourWheeler");    }}class Car extends FourWheeler{    public Car()     {        System.out.println("Class Car");        }}class Audi extends Car{   public Audi()    {        super();        System.out.println("Class Audi");    }}class Driver{    public static void main(String args[])    {        Audi cc=new Audi();    }}

public class Employee {private int id;private String name;private float salary;private String pancardno;public Employee(){System.out.println("In default constructor");}public Employee(int id, String name, float salary) {this.id = id;this.name = name;this.salary = salary;}public Employee(int id, String name, float salary, String pancardno) {this(id,name,salary); this.pancardno = pancardno; }this();}

You have a class Container:public class Container{ public int myInt = 3; public int getMyInt(){ return myInt; }}Then you execute the following code:Object myContainer = new Container();myContainer.getMyInt();What is the error in this code?

You have a class Student:public class Student { public int studentId; public String studentName; public String studentGrade; public Student() { studentId = 1; studentName = null; studentGrade = "1st"; }}You have a child class CollegeStudent:public class CollegeStudent extends Student {}Then you execute the following snippet of code:CollegeStudent myCollegeStudent = new CollegeStudent();System.out.println(myCollegeStudent.studentName);What will be returned by this code?Instruction: Choose the option that best answers the question. Answer ChoicesAn exception will be thrown indicating there is no studentName variable in the CollegeStudent derived classAn exception will be thrown indicating that the studentName variable is not accessible in the derived class CollegeStudentfalsenull

You have a class Student:public class Student { public int studentId; public String studentName; public String studentGrade; public Student() { } public Student(int studentId, String studentName, String studentGrade){ this.studentId = studentId; this.studentName = studentName; this.studentGrade = studentGrade; }}You have a child class CollegeStudent:public class CollegeStudent extends Student { public CollegeStudent(int studentId, String studentName, String studentGrade){ System.out.println("This constructor uses the super keyword"); super(studentId, studentName, studentGrade); }}What is the error in the definition of the constructor in CollegeStudent?Instruction: Choose the option that best answers the question. Answer ChoicesYou have to use the super() method in the first line of the constructor bodyDerived classes cannot invoke constructors that are part of the parent classYou have to use the base() method to invoke the constructor of the superclassYou must use the modifier “constructor” to define constructors

1/3

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.