-2

I am a Java noob so kindly forgive me if the question is too simplistic or too complex but I have been going through my Java book and in the first chapters we covered Inheritance, and later on we came to Interfaces.

My questions are: When is it right to use an interface and when is it right to override methods instead?

Since we already have overriding functionality, why do we need Interfaces?

Ryan
  • 9,821
  • 22
  • 66
  • 101

5 Answers5

1

Have a look at this page: http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html. It explains some of the differences, and when you should use each.

Or this one: http://www.javabeginner.com/learn-java/java-abstract-class-and-interface. This one's better, I think.

OOkhan
  • 297
  • 3
  • 8
  • 20
1

Think of it like this: Consider a Car class, that serves as basis for several classes like Pickup, MiniVan and SUV, where you override Car- methods.

So far, so good.

But as in real life, a Car is not just a Car. It is also, for example, a physical body. Hence, depending on your application, you'll need to look at Airplanes, Cars, and maybe several other things as just physical bodies, who all have something in common, for example:

double getMass();
double getVelocity();

At the same time, a Car is not just a physical body, but also a household asset. It may be associated with a Loan, for instance. Hence, you might want to support method like:

Loan getLoan();

Thus, interfaces can serve to provide certain views upon objects of different classes as if those objects had the same type.

Ingo
  • 36,037
  • 5
  • 53
  • 100
1

Interfaces provide some kind of contract, which the class should implement. Let's say you have an interface called Car, which has a accelerate() method. No implementation here.

Now, you want some real usages of Car, so you create a SubaruCar class, which implements accelerate() method.

Now, if you for example need a SubaruImpreza class, it would probably extend SportCar using inheritance and the keyword extends. In this class, you override accelerate() method (because it will accelerate even faster then usual SubaruCar)

Eel Lee
  • 3,513
  • 2
  • 31
  • 49
1

The net is full with documentations regaring this issue, but if I try to summerize it in simple english than:

Interface - It's like a contract that says "I can supply this functionality". example of interface: Comparable that means that any comparable obejct can supply functionality of comparing

Overreid - Is when you use inharitance and want some functionality to act differently on the child class.

those are two diffrent issues that on the beggining can be somehow confusing

yossico
  • 3,421
  • 5
  • 41
  • 76
1

Others here have made good points, but let me offer a slightly different perspective.

If you have behavior (not code or traits, mind you, but actual behavior) that is common among objects, consider an abstract class with some established behavior in methods (possibly declared final). But be careful. Look up the Circle-Ellipse problem to see why Circle probably shouldn't inherit from Ellipse even though that seems obvious to do.

You can only extend one class, so make it a good one. Keep in mind though that good software design is about coupling your code as loosely as possible. In other words, be as sure as possible that changing the code in one class doesn't involve changing the code in a bunch of other classes. That's why working over the weekend happens. There is no tighter coupling than inheritance, so choose wisely. Adding new behavior to a base class means every derived class gets it too, and that can lead to a lot of awkward problems (solved, for example, by implementing an empty method just to satisfy the contract).

Finally, the ability to extend multiple interfaces allows client code to treat an object differently according to a different API contract, so I can treat a Person object as a Doctor in one part of the code and a Father in another part of the code. But make no mistake, this is not multiple inheritance.

In the end, it depends on your domain, the possibility of changes to your base classes, and other factors that you will learn to measure with experience. All things being equal though, prefer interfaces to inheritance.

And wait until you learn Ruby and Scala and you get to learn about modules and traits. Then it gets really fun.

Hope that helps.

Vidya
  • 29,932
  • 7
  • 42
  • 70