Create an interface called StudentInterface. This interface has the following abstract method:
public void study(String subject);
The Student class implements this interface, and provides an implementation for study() which displays that the student is studying a certain subject.
(Note what happens if you don’t implement study().)
interface StudentInterface {
void study(String subject);
}
class Student implements StudentInterface {
// If `study` method is not implemented, then a compilation error happens
public void study(String subject) {
System.out.println("The student is studying " + subject);
}
}
class Test {
public static void main(String[] args) {
Student student = new Student();
student.study("Math");
}
}
Comments
Leave a comment