Reputation: 31
I am trying to understand enums in Java, but currently can not find a post that explains the question I am asking.
I have a Person
interface and a PersonEO
class. Two methods of the Person Interface are setGender and getGender. I would like to hold gender as an enum.
Do I create the enum in the interface and have it get carried over to the class or do I make the enum in the class?
This is currently what I have been trying but can not get the enum to pass over to the class properly and don't really understand how it will flow through the program.
public interface Person{
public enum gender{
Female,
Male,
Unknown;
private String gender;
public String getGender(){
}
void setGender(){
}
}
------------------------------------------------------
public class PersonEO implements Person {
//Both override methods are not seen as methods from the interface
@Override
public String getGender() {
return gender;
}
@Override
public void setGender() {
//Not sure how to set the gender from the enum
this.gender = gender;
}
}
Please show an example.
Upvotes: 1
Views: 2624
Reputation: 50776
The enum, interface and class are all separate types and should usually be in separate files. Here are some sample definitions:
enum Gender {
MALE, FEMALE, UNKNOWN;
}
interface Person {
void setGender(Gender gender);
Gender getGender();
}
class PersonImpl implements Person {
private Gender gender;
@Override
public void setGender(Gender gender) {
this.gender = gender;
}
@Override
public Gender getGender() {
return this.gender;
}
}
Upvotes: 6