Knowee
Questions
Features
Study Tools

Fixing Error(s)In the above question, you identified the error(s) in the constructor for the University class which is a part of the code given below. How will you now fix the error(s)?class University extends School { private final String type = "University"; private boolean isPublic; public University(String name, int numStudents, boolean isPublic) { this.name = name; this.numStudents = numStudents; this.isPublic = isPublic; } public boolean getIsPublic() { return this.isPublic; }}

Question

Fixing Error(s)In the above question, you identified the error(s) in the constructor for the University class which is a part of the code given below. How will you now fix the error(s)?class University extends School { private final String type = "University"; private boolean isPublic; public University(String name, int numStudents, boolean isPublic) { this.name = name; this.numStudents = numStudents; this.isPublic = isPublic; } public boolean getIsPublic() { return this.isPublic; }}

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

Solution

The error in the code is that the class University is trying to directly access the private fields of the parent class School. In Java, private fields are not inherited by the child classes and cannot be accessed directly.

To fix this error, you should use the constructor of the parent class School to initialize the fields. If the constructor is not available in the parent class, you should create it. Here is the corrected code:

class School {
    private String name;
    private int numStudents;

    public School(String name, int numStudents) {
        this.name = name;
        this.numStudents = numStudents;
    }

    // getters and setters for name and numStudents
}

class University extends School {
    private final String type = "University";
    private boolean isPublic;

    public University(String name, int numStudents, boolean isPublic) {
        super(name, numStudents); // calling the constructor of the parent class
        this.isPublic = isPublic;
    }

    public boolean getIsPublic() {
        return this.isPublic;
    }
}

In the above code, the constructor of the University class calls the constructor of the parent class School using the keyword 'super'. This allows the University class to initialize the name and numStudents fields of the School class.

This problem has been solved

Similar Questions

DebuggingLook at the code for the School and University classes given below. There is an error in the constructor for the University class. Can you identify the error?class School { private String name; private int numStudents; public School(String name, int numStudents) { this.name = name; this.numStudents = numStudents; } public String getName() { return this.name; } public int getNumStudents() { return this.numStudents; }}class University extends School { private final String type = "University"; private boolean isPublic; public University(String name, int numStudents, boolean isPublic) { this.name = name; this.numStudents = numStudents; this.isPublic = isPublic; } public boolean getIsPublic() { return this.isPublic; }}

Method OverridingLook at the code given below for the School and University classes.class School { protected String name; protected int numStudents; public School(String name, int numStudents) { this.name = name; this.numStudents = numStudents; } public String getName() { return this.name; } public int getNumStudents() { return this.numStudents; }}class University extends School { private final String type = "University"; private boolean isPublic; public University(String name, int numStudents, boolean isPublic) { super(name,numStudents); this.isPublic = isPublic; } public boolean getIsPublic() { return this.isPublic; } public String getName() { return super.getName() + " " + type; } public int getNumStudents(){ return super.getNumStudents(); }}If you create a University object called ‘univ’ (short for university) and call the getName() method on the univ object, which getName() method will be called?Note: Look at the code closely; the getName() method is defined in both the School and University classes.getName() method of the School classgetName() method of the University class

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 {}Then you execute the following snippet of code:CollegeStudent myCollegeStudent = new CollegeStudent(1, "Alice", "3rd");What is the error in this snippet of code?Instruction: Choose the option that best answers the question. Answer ChoicesYou cannot specify multiple constructors for the parent class, though this is allowed in child classesYou cannot use the parameterized constructor in the base class to instantiate the CollegeStudent derived classYou need to have additional constructors in the base class to support the instantiation of the CollegeStudent derived classYou have to use the “object” keyword to create an object of a derived class

You have a class Student:public class Student { public int studentId; public String studentName; public String studentGrade; 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 {}What is the error in this inheritance setup?Instruction: Choose the option that best answers the question. Answer ChoicesYou have to specify a default, no argument constructor in the derived class so that the derived class can be instantiatedYou have to create child classes marked with the special modifier “derived” so the inheritance hierarchy itself is wrongYou have to specify a parameterized constructor in the derived class so that the derived class can be instantiatedThere has to either be a default constructor defined in Student or a constructor defined in CollegeStudent

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.