Parent Class Error in Java

Hello Everyone, I am working on a java project and getting some errors which I mentioned below. I have this class, Person:

public class Person{
  String firstname;
  String lastname;

  public Person(String fname, String lname){

  }
  public String toString(){

  }
}

And this subclass, Student:

public class Student extends Person{
  Student(){
    super();
  }
  int studentID;
  int level;

  public Student(String fName, String lName, int gLevel){

  }
  public int getLevel(){

  }
  public String toString(){

  }
}

When I compile this code on Interviewbit, I get the error:

cannot find symbol 
symbol  : constructor Person() 
location: class Person

I am confused what the problem is. It’s my understanding that the use of the super() constructor should resolve this problem, and that it’s not even necessary in the code but I’m continuing to get this error.

Because Person has no defaut constructor. public Person(String fname, String lname){} has overwritten defaut constructor. Defaut constructor is public Person(){}.

super() (without parameter) calls to method public Person(){} but it doesn’t exist.

You can view this topic with google translate.

and these 3 articles

https://www.quora.com/In-Java-does-every-object-constructor-automatically-call-super-in-object-before-its-own-constructors

3 Likes

A question for you

public class Person {
	protected static int count = 0;
	String firstname;
	String lastname;

	public Person() {
		count++;
	}

	public Person(String fname, String lname) {
	}

	public String toString() {
		return null;
	}
}



class Student extends Person {

	public Student(String fName, String lName, int gLevel) {
		count++;
	}

	public int getLevel() {
		return 0;

	}

	public String toString() {
		return null;
	}

	public static void main(String[] args) {
		System.out.println(count);
		Student st = new Student(null, null, 0);
		System.out.println(count);
	}
}

What does the console output?

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?