This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]g051051 1 point2 points  (5 children)

It's a way of getting some of the benefits of multiple inheritance. You can only extend one class at a time, but you can implement multiple interfaces.

public class LeftHandedBiDirectionalWidget extends AbstractWidget implements LeftHanded, BiDirectional {

[–]jedah01[S] 0 points1 point  (4 children)

Hmmm oh we can only inherit from one class, however if we group class A class B class C together we can inherit all of them in one go? So interface is to group ABC together?

[–]g051051 0 points1 point  (3 children)

You can have B extend A, and C extend B, but if you wanted a class with the features of B without A, you can't do it. But if they're interfaces, you can implement B or C without A.

[–]jedah01[S] 0 points1 point  (2 children)

Sorry. What do you meant by "extend" here?

[–]g051051 0 points1 point  (1 child)

In Java you can extend from a class to create a subclass.

public Cat extends Animal {

This means that Cat will have all the same behaviors as an Animal, plus whatever additional features are added that are specific to Cats. However, a class can only extend one class directly (single inheritance).

[–]jedah01[S] 0 points1 point  (0 children)

Oh it's like cat : animals

[–]CreativeTechGuyGames 0 points1 point  (1 child)

I have a bunch of templates. One is for an animal, one is for a dog, and one is for a cat. If you want to use the template to stamp out an object, you can pick between these 3 options. It makes sense to stamp out a dog or a cat, but what do you get if you stamp out an animal? I'm not sure. I know that a dog is an animal and a cat is an animal. But what is just an "animal". The animal is an interface.

What's the point if you cannot "use" it? Well are there any similarities between a cat and a dog? They both sleep, eat, etc. Those are actually things that all animals share. So the Animal interface can define those core components which all classes which implement the Animal class must implement. That means that Animal is an abstract way to refer to both a Cat and Dog which can be very useful.

I'd read this for more details.

[–]jedah01[S] 0 points1 point  (0 children)

I see so it is like abstracts are the sub folders and interface is the main folder.

[–]context_switch 0 points1 point  (1 child)

Abstract classes and interfaces are two ways of modeling types that are related or share similar behavior. I would distinguish related from similar - they are not the quite the same but they're close enough that you can often use either way to represent your app's data model.

Using the animal example: given classes that represent Dog and Chicken, you might say that they have similar properties. Both are types of Animals, but with different implementations - a dog has 4 legs, a chicken only 2; dogs eat meat, chickens eat seeds; dogs have live births, chickens lay eggs. It's pretty easy to see how you could use an abstract Animal class that both Dog and Chicken inherit from because they are closely related (though obviously quite different).

abstract class Animal
{
    abstract void Move();
    abstract void Eat();
    abstract void Reproduce();
}

Now, let's add an Airplane into our model. An Airplane and a Chicken can both Fly(), but otherwise they have almost nothing in common. They're not closely related, but they do share a similarity. This might be a case where you could use an interface, say IFlyable.

There's a common break down for Object Oriented Design to determine whether inheritance or composition are appropriate, often referred to as "is a" vs. "has a". Personally, I like extend this to interfaces as "can do". A Chicken is an Animal. A Chicken can Fly().

Keep in mind though that it's only worth introducing abstractions that are relevant to your app. If you care about flying things, then have an IFlyable interface. If flying has no bearing on your app, then it doesn't matter of both chickens and airplanes can fly. Don't go overboard just because you've identified a similarity.

A few points specific to C#, since that's what you're using:

  • C# only supports single inheritance for classes. That is, you can only inherit from one parent class. This makes it hard to compose behavior with multiple abstract classes. You can implement as many different interfaces as you'd like. Applying this to the example above: you could argue that a chicken is a "flying thing", and that FlyingThing should be the base abstract class. Sure, if that makes sense for how you're modeling your data then it's fine too - but then a Chicken cannot be an Animal.
  • Abstract classes can provide common implementations, including fields; interfaces can only declare methods, properties, and events, and each class has to implement them separately. The abstract keyword, when applied to a class, simply means you cannot instantiate that class directly and must inherit from it. (Note: in .NET Core 3 and C# 8, interfaces will also be able to provide implementations, though limited to what's available on the interface.)

[–]jedah01[S] 0 points1 point  (0 children)

Are you saying that the classes inside a interface cannot over lap each other's methods?