I am a beginner to Java and object-oriented programming and having some difficulty with the concepts. For homework, I need to write two different classes that have the same exact API but are implemented differently. What does that mean and how does that work?
I will show you.This is an example that two class have same api.
interface ISpeak {
void sayHi();
}
class Teacher implements ISpeak{
@Override
public void sayHi() {
System.out.println("Hi!I am a Teacher!");
}
}
class Student implements ISpeak{
@Override
public void sayHi() {
System.out.println("Hi!I am a Student!");
}
}
Same API means the two classes contain the exact same list of public methods (each having the same method name and method parameters as the other class). The implementation of these methods can be different in each class. In addition, each class can also having private methods that don't appear in the other class, since private methods are not part of the API that a class provides to its users.
An API is usually defined in Java by an interface, so two classes that have the same API will usually implement the same interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With