Knowee
Questions
Features
Study Tools

Method OverridingIn the previous question, you must have chosen either getName() of the School class or getName() of the University class. Can you explain the reason behind your choice?

Question

Method OverridingIn the previous question, you must have chosen either getName() of the School class or getName() of the University class. Can you explain the reason behind your choice?

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

Solution

Sure, the concept you're referring to is called "Method Overriding" in Object-Oriented Programming (OOP). This is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by its parent class or superclass.

When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the subclass will be executed.

In your case, if you have a getName() method in both the School class and the University class, and you create an object of the University class then call this method, it will execute the getName() method in the University class. This is because the University class is a subclass of the School class and you've overridden the getName() method in the University class.

This problem has been solved

Similar Questions

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

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; }}

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; }}

You have a class Student with a method viewStudentInfo():public class Student { // Assume the constructors are set up correctly protected int studentId; protected String studentName; protected int studentGrade; public void viewStudentInfo(){ System.out.format("Id: %d, Name: %s, Grade: %d" , studentId, studentName, studentGrade); }}You have a child class CollegeStudent with an overridden implementation of viewStudentInfo :public class CollegeStudent extends Student { // Assume the constructors are set up correctly public void viewStudentInfo(){ System.out.format("Year: Freshman, Id: %d, Name: %s, Grade: %d" , studentId, studentName, studentGrade); }}Then you execute the following snippet of code (assume that the constructors are set up correctly and this class instantiation works):Student bob = new CollegeStudent(1, "Bob", 50);bob.viewStudentInfo();What will be returned by this snippet of code?Instruction: Choose the option that best answers the question. Answer ChoicesYear: Freshman, Id: 1, Name: BobYear: Freshman, Id: 1, Name: Bob, Grade: 50Year: Freshman, Id: 1, Name: Bob Id: 1, Name: Bob, Grade: 50Id: 1, Name: Bob, Grade: 50

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

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.