Friday, August 28, 2009

Published 8/28/2009 by with 0 comment

cyclic inheritance involving ...

public class Person extends Employee
{
private String firstName;
private String lastName;

public Person(String firstName,String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}

}

Person is a class which inherits Employee and see the code below where Employee also inherit the Class Person.


public class Employee extends Person
{
private double salary;

public Employee(String firstName,String lastName,double salary)
{
super(firstName,lastName);
this.salary=salary;
}
}

In the above code we will see the error message "cyclic inheritance involving Person

public class Person extends Employee"

Actually Person should be the super class here and Employee is the subclass. But here the relationsship is such that Employee is the subclass of Person, again Person is the subclass of Employee. It's a cyclic inheritance which is not possible.

The solution of this problem is identifying the proper relationship i.e, finding out the super class and the subclass. In our case Person is the super class, so it should not try to inherit Employee and Employee is the subclass which will inherit Person. Correction is ommiting "extends Employee" from the Person class signature as below:

public class Person
{
private String firstName;
private String lastName;

public Person(String firstName,String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}

}

public class Employee extends Person
{
private double salary;

public Employee(String firstName,String lastName,double salary)
{
super(firstName,lastName);
this.salary=salary;
}

}
    email this       edit

0 comments:

Post a Comment