6. There is one error, in the code below; underline the line that has an error a
ID: 3856614 • Letter: 6
Question
6. There is one error, in the code below; underline the line that has an error and specify what the error is
public abstract class A {
private int x;
public void setX(int x) { this.x = x; }
public abstract void findIt();
public String toString() { return "X: "+x; }
}
public class B extends A {
private int w = 1;
public String toString() { return "w: "+w; }
public void findIt(){ w = w + w;}
}
public class C {
public static void main(String[] args) {
A a = new A();
B b = new B();
System.out.println(b.toString() );
}
}
Answer:
public class Student {
private int sid;
protected String name;
public double gpa;
public int getSid() {
return sid;
}
public String toString() {
return "Name: " + name + "Gpa: "+gpa;
}
protected double number() { return gpa; }
}
public class TA extends Student {
private double salary;
public double number() { return salary; }
}
7. What are all the methods that can be accessed/invoked in TA?
8. What are all the methods that are overrriden in TA?
()
9. write a method toString for class TA that will return a string with all the its instance variables from its class and its parents class.?
public String toString() {
return super.toString() + salary;
}
Explanation / Answer
Hi buddy, please find the below answers
6. A a = new A(); in Main method in class C. Abstract class can't be instantiated. Here A is an abstract class.
7. public int getSid() and public String toString() can be accessed for sure as they are public methods. Public methods can be accessed from any other class by creating the object.
protected double number() method can also be accessed from the TA class as TA class is sub class of student class. Protected methods can only be accessed by the sub classes.So, all the methods present can be accessed from TA class
8. public double number() method is overriden in the TA class as the method with the same signature is present in the super class (Student)
9. @Override
public String toString() {
return "Name: " + name + "Gpa: " + gpa+" Sid : "+sid;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.