I was creating a Student class which is like this
public class Student {
protected String name;
protected String surName;
protected long id;
protected String email;
}
public interface IGraduated {
public void doSmth();
}
public class GraduatedStudent extends Student implements IGraduated{
private String companyInfo;
private double salaryInfo;
@Override
public void doSmth() {
System.out.println(this.name + " is working on the " + this.companyInfo);
}
I wanted the doSmth method to be specific for the graduate students. So I seperated it to a interface.
And I want to use like this, I know its impossible and not suitable for oop design. But is there anything can I do in this case?
Student stu = new GraduatedStudent();
stu.doSmth();
I could be wrong here, but if you do not explicitly need an Interface, I wouldn't go that route for the endgame you're going with.
Instead of using an interface to declare only one method specific to one class, I would simply declare the method in the child class and go from there :
I know it could be optimized but it's the general idea that counts. An Interface should be used when you have (or you know you will have) multiple classes with similar methods. Nothing in your logic is necessarily wrong, but it's a little too complicated for what you need to accomplish.
Also, I know not everybody sees it that way, but try to only use protected when necessary. It can open holes in your code (certain classes/methods being able to change the values of those variables).