Hi everyone, I'm a fairly new Java student and I need some help regarding subclass.

This is what I'm trying to do:

Basically, I created a Superclass called Person, and from it I created a subclass called CollegeMember. Now, I need to divide the CollegeMember subclass to "Lecturer", "Student" and "Technicians". (Please note that I cut off the methods since I don't think they are needed in this case.)

Person (superclass):

public class Person
{
    public Person() //constructors
    {
        
    }
    public Person(String n, String s, int a, int id, double w)
    {
        setName(n);
        setSurname(s);
        setAge(a);
        setId(id);
        setWeight (w);
    }
    
    private String name; // instance variables
    private String surname;
    private int age;
    private int id;
    private double weight;
}

CollegeMember: (subclass extending Person)

public class CollegeMember extends Person
{
    public CollegeMember(String name, String surname, int age, int id, double weight, String collegeName)
    {
       super(name, surname, age, id, weight);
       setCollegeName(collegeName);        
    }
    
    private String collegeName;

    public String getCollegeName() 
    {
        return collegeName;
    }

    public void setCollegeName(String collegeName) 
    {
        this.collegeName = collegeName;
    }
    
    public String attendAtTme(double time)
    {
        String aat = getName()+ " " + getSurname() + " is attending at " + time ; 
        return aat;
    }
}

Lecturer: (subclass of CollegeMember - please note that I have probably attempted the following code a few ten times, and I can't seem to get this right. So I just pasted my first attempt. )

public class Lecturer extends CollegeMember
{
    public Lecturer()
    {
        
    }
    
    public Lecturer(String n, String sn, int a, int id, double w, String cn, String d)
   { 
     
    }
    
    private String degree;
}

The error is in the brackets marked in red. "constructor CollegeMember cannot be applied to given types."

So, anyone has some free time to spare and try to help me out?

Thanks in advance!

Regards,
Tsuchino

Dani AI

Generated

Short tip expanding on ’s diagnosis and how to fix it cleanly.

Java always inserts an implicit super() call as the first line of a constructor when you do not call this(...) or super(...) yourself. If the direct superclass has no matching no-arg constructor, the compile error you saw appears. Two practical, safe fixes:

  • Make the subclass call the existing superclass constructor explicitly. Example for Lecturer:

    public Lecturer(String n, String sn, int a, int id, double w, String cn, String d) {
      super(n, sn, a, id, w, cn); // call CollegeMember(...)
      this.degree = d;
    }

    You can provide a no-arg Lecturer by delegating to the parameterized one:

    public Lecturer() {
      this("Unknown","Unknown",0,0,0.0,"UnknownCollege","");
    }
  • Or add a no-arg constructor to CollegeMember so an implicit super() from Lecturer() can succeed:

    public CollegeMember() {
      super(); // calls Person()
      this.collegeName = "";
    }

Extra notes and common pitfalls: the super(...) (or this(...)) call must be the very first statement. The compiler will not auto-generate a default no-arg constructor if any other constructors are declared, so add one explicitly if you need it. Keep field visibility in mind — private fields in Person remain inaccessible to subclasses (use getters/setters or protected if direct access is desired). Using the IDE’s “Generate constructor” tool often produces correct chaining and avoids signature mismatches.

Recommended Answers

All 2 Replies

You didn't provide CollegeMember with a zero-argument constructor, which your Lecturer constructors are trying to call implicitly.

Dumb me..... Thanks a bunch Ezzaral.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.