-5

I am trying to access a constructor in an abstract class that is two levels higher.

    public abstract class Person{
        protected String name;

    public Person(String name){
         if name.length() <= 12)
            this.name = name;
         else 
            this.name = name.substring(0,12);
    }

    public final String returnName(){
       return name;
    }
   }

public class employee extends person{

        public employee(string firstname, string gender){
               super(firstname);
               this.gender =gender;
         }

}

public class dependent extends employee{

        public dependent(string firstname, string gender, string relation){
             super(firstname);
             super(gender);
             this.relation = relation;
        }

How do I invoke the constructor of the abstract class from the dependent class (two levels below)?

Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51

3 Answers3

0

try this:

public class employee extends Person{

        public employee(String firstname, String gender){
               super(firstname);
               this.gender =gender;
         }

}

public class dependent extends employee{

        public dependent(String firstname, String gender, String relation){
             super(firstname,gender);
             this.relation = relation;
        }
Mailkov
  • 1,231
  • 1
  • 13
  • 17
0

Ok, first of all, here's how you pass two parameters to your Employee constructor:

super(firstname, gender);

Second, there's no need to call the Person constructor from the Dependent one. This will happen automatically when Dependent calls the Employee constructor, because the Employee constructor then calls the Person one.

yts
  • 1,890
  • 1
  • 14
  • 24
0

What you are looking for is something like the below, please note Classes start with Upper-case and camel case for method parameter staring with a lower-case letter. String must be upper-case String name , please also be aware you have introduced a protected member scope on name See the following post on the implications In Java, difference between default, public, protected, and private

  public abstract class Person
  {
    protected String name;

    public Person(String name)
    {
      if (name.length() <= 12)
      {
        this.name = name;
      }
      else
      {
        this.name = name.substring(0, 12);
      }
    }

    public final String returnName()
    {
      return name;
    }
  }

  public class Employee extends Person
  {
    private String gender = null;

    public Employee(String firstName, String gender)
    {
      super(firstName);
      this.gender = gender;
    }

  }

  public class Dependent extends Employee
  {
    private String relation = null;

    public Dependent(String firstName, String gender, String relation)
    {
      super(firstName, gender);
      this.relation = relation;
    }
  }

You will need to add methods to access the relation and gender if you need the access to these

Community
  • 1
  • 1
Kenneth Clark
  • 1,725
  • 2
  • 14
  • 26