1

This concept is unclear with me.

I have worked on several frameworks for an instance Spring. To implement a feature we always implement some interfaces provided by the framework.

For an instance if I have to create a custom scope in Spring, my class implements a org.springframework.beans.factory.config.Scope interface. Which has some predefined low level functionality which helps in defining a custom scope for a bean.

Whereas in Java I read an interface is just a declaration which classes can implement & define their own functionality. The methods of an interface have no predefined functionality.

interface Car
{
topSpeed();
acclerate();
deaccelrate();
}

The methods here don't have any functionality. They are just declared.

Can anyone explain this discrepancy in the concept? How does the framework put some predefined functionality with interface methods?

  • How does the framework put some predefined functionality with interface methods? -- By providing an implementation for that interface, just like you do when you write a class that inherits the interface. – Robert Harvey Jan 27 '14 at 17:10
  • In Java SE 7 (it's different in Java 8), a Java class is single inheritance. Mercedes can only extend Car, cannot extend LuxuryVehicle. However, Mercedes can implement multiple interfaces. Google on "diamond inheritance problem", "multiple inheritance" and "favor composition over inheritance". Java 8 has a sort of multiple inheritance, I think, making it more like C++ in terms of diamond inheritance problem... – Thufir Mar 13 '15 at 13:39
1

There is no difference between how a framework handles interfaces and how your code does. There is a concept called Interception. Frameworks often times have classes that intercept calls on interfaces and run their own code before or after your implementation runs.

A simple roll-your-own way to do interception in a framework that you are writing would be to use the decorator pattern. Many DI frameworks provide a means of doing interception and use more sophisticated ways of accomplishing it such as through the use of reflection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.