I have an entity which has info1 or info2 dependently from its type, but only one of these infos should be filled. These infos have absolutely different properties. So my entity looks like this:
public class SomeEntity {
private SomeEntityType type;
private Info1 info1;
private Info2 info2;
}
So my question is this Ok to create some empty abstract class Info and inherit Info1 and Info2 from it? (Since I think that the number of such Info classes can grow and I will need add all such infos to SomeEntity)
public class Info1 extends Info {
/* some properties */
}
public class Info2 extends Info {
/* some properties */
}
public class SomeEntity {
private SomeEntityType type;
private Info info;
}
In a word - yes, it is. However, it may be more idiomatic to use an empty interface and have Info1 and Info2 implement it rather than extend an empty class.
Yes of course. That's why there is abstract class. You can create abstract class Info and write some abstract methods. You're going to implement those methods in classes Info1 and Info2.
Example
public abstract class Info {
abstract void getInfo();
}
public class Info1 extends Info {
void getInfo() {
System.out.println("This is Info 1");
}
}
public class Info2 extends Info {
void getInfo() {
System.out.println("This is Info 2");
}
}
As you can see, I implemented methods differently in Info1 and Info2.