I have seen the term "common interface" used a lot while reading books about OOP.
For example, the book The Essence of Object-Oriented Programming with Java and UML says the following:
Abstract classes usually define a common interface for subclasses by specifying methods that all subclasses must override and define
My understanding of the term "common interface" is the following:
Assume that we have a superclass (or an interface
or an abstract class
) called Animal
and two subclasses called Dog
and Cat
, and Animal
have two virtual methods called makeSound()
and move()
.
Now the common interface would be composed of two methods which are Animal.makeSound()
and Animal.move()
.
Assume that we have the following code:
Animal animal1 = new Dog();
animal1.makeSound();
animal1.move();
animal1 = new Cat();
animal1.makeSound();
animal1.move();
The explanation of the above code is the following:
Animal animal1 = new Dog()
creates an Animal
common interface and associate a Dog
object with it:
animal1.makeSound()
sends an Animal.makeSound()
message to the common interface, and then the common interface sends a Dog.makeSound()
message to the Dog
object:
Same thing happens in the case of animal1.move()
(which is the Animal.move()
message is sent to the common interface, etc.).
animal1 = new Cat()
removes the Dog
object from the common interface, and associate a Cat
object with the common interface:
animal1.makeSound()
sends an Animal.makeSound()
message to the common interface, and then the common interface sends a Cat.makeSound()
message to the Cat
object:
Same thing happens in the case of animal1.move()
(which is the Animal.move()
message is sent to the common interface, etc.).
Am I correct in my understanding?
Edit:
The Animal
common interface in my diagram is not an object, but rather it is the virtual methods (or I guess I can call them the "message handlers") that receive the Animal.makeSound()
and Animal.move()
messages and then send the appropriate messages to the associated subclass object.
The Animal common interface in my diagram is not an object, but rather it is the virtual methods (or I guess I can call them the "message handlers") that receive the Animal.makeSound() and Animal.move() messages and then send the appropriate messages to the associated subclass object. Does this make my diagram correct now?
Due to this comment, I considered removing my answer because I find a meaningful difference between the question What does “common interface” mean in OOP? and Ho are interface calls managed in runtime by the JVM.