-3

Given the following classes, complete the code for the Student class so that the following output is printed when we run the Q01 class. Hint: Student class is a child class of Person

Here is the output:

Department: CSE, ID: 20101001, Name: Mark Wahlberg

Department: BBA, ID: 20101002, Name: Zeeko Zaki

Department: CSE, ID: 20101002, Name: Zeeko Zaki

public abstract class Person {
  private String name;
  private Person() {
  }
  Person(String name) {
    this.name = name;
  }
  protected String getName() {
    return name;
  }
  protected void setName(String name) {
    this.name = name;
  }
}
public class Q01 {
  public static void main(String[] args) {
    Student s1 = new Student("CSE", 20101001, "Mark Wahlberg");
    Student s2 = new Student("BBA", 20101002, "Zeeko Zaki");
    s1.printDetail();
    s2.printDetail();
    s2.setDepartment("CSE");
    s2.printDetail();
  }
}

I created a new class below the class

public class Student extends Person{
  public String department="";
  public int id;
  public String name;
  public String Public(String name){
    return name;
  }

  public Student(String department,int id,String name){
    this.department=department;
    this.id=id;
    this.name=name;
  }
  public void setDepartment(String department){
    this.department=department;
  }
  public String getDepartment(){
    return department;
  }
  public void printDetail(){
    System.out.println("Department: "+this.department+", ID: "+this.id+", Name: "+name);
  }
} 

I cannot compile this code. How to solve this problem? where are my errors?

hat
  • 781
  • 2
  • 14
  • 25
  • "I cannot compile this code." - Why not? – Jacob G. Aug 19 '19 at 14:49
  • What errors did you get? – hat Aug 19 '19 at 14:50
  • 1
    It looks like there are several errors in the Student class starting at line 5 – Sebastian Aug 19 '19 at 14:50
  • 1
    `public String Public(String name){` Bad naming convention. Do not use java keywords to name your attributes, members etc... I am gonna assume it is getter therefore you should name it `public String getName()` – Yoshikage Kira Aug 19 '19 at 14:52
  • Tip: to format a code block, just highlight all of it and click the `{}` button or press `ctrl`+`k`. You don't need any html formatting for paragraphs. ` is for a single line. – hat Aug 19 '19 at 14:55

1 Answers1

1

The constructor for Student needs to call the constructor of the parent class first.

To do this you need to call 'super' and pass the correct parameters, in this case the parent class (Person) has 2 constructors, the default constructor is private and only accessible within the Person class so you have to use the second constructor for the subclass Student, which takes a String name. e.i.

 public Student(String department,int id,String name){
    super(name);
    this.department=department;
    this.id=id;
    this.name=name;
}
Pete
  • 205
  • 2
  • 14
  • Hope it goes without saying that you must also remove the `enter code here` to get this to compile – Pete Aug 19 '19 at 14:57
  • That was from a code formatting mistake in the question editor, I have suggested an edit to fix it. – hat Aug 19 '19 at 14:58