Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean for two classes have the same API but different implementations?

Tags:

java

object

oop

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?

like image 858
user1454994 Avatar asked Dec 27 '15 09:12

user1454994


2 Answers

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!");
    }
}
like image 176
xylsh Avatar answered Sep 26 '22 14:09

xylsh


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.

like image 27
Eran Avatar answered Sep 24 '22 14:09

Eran