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;
}

}
Read More
    email this       edit

Wednesday, August 19, 2009

Published 8/19/2009 by with 5 comments

class, interface, or enum expected

You see this error message because of any one of the followings:

1. You misspelled the keyword class (fully lower case) or you did not write it at all for writing your class.

public Class Test //actual keyword is class
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}

In the above example Class (C is in upper case) is written instead of class (fully lower case).

2. Remember you cannot declare variables outside of class body like below.

int variable; //variable must be declared inside the class body
public class Test
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}

To solve this declare variables inside the class body as below.


public class Test
{
int variable; //variable must be declared inside the class body
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}


3. All methods are also defined within the method body. If you define any method outside of the class body you will see this error message.


public class Test
{
public void myMethod()
{
System.out.println("method inside the class body");
}
} //end of class

public void anotherMethod()
{
System.out.println("method outside of the class body");
}

In the above code, the method anotherMethod is defined after the end of the class. So this error occurred. Most often it occurs just because of incorrect use of {} (curly braces). Check all { are closed with } in the proper place.

4. If you are defining an interface, check you spelled it correctly in fully lowercase.

public Interface Test //actual keyword is interface
{
public void myMethod();

}

In the above code, keyword interface is misspelled with upper I.

5. Same way if you define an enum spell the keyword enum correctly.

public Enum Day //actual keyword is enum
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

In the above example Enum is incorrect as the E is in upper case, it will be lower case e.
Read More
    email this       edit