Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any real example of using interface related to multiple inheritance

I m trying to understand Interfaces so that I can implement them in my programs but I m not able to imagine how should i use them. Also give me some eg of using them with multiple inheritance in C#

like image 825
Shantanu Gupta Avatar asked Dec 18 '09 18:12

Shantanu Gupta


People also ask

Is interface is an example of multiple inheritance?

An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.

What is example of interface in real life?

The Interface is a medium to interact between user and system devices. For example, in our real life, if we consider the interface to be given as an example is the air condition, bike, ATM machine, etc.

What is multiple inheritance with real time example?

Multi-Level Inheritance We can take the example of classes such as class food, class pizza, class coca-cola so here parent class is food class and class pizza extended to coca cola class. It comes in types of inheritance in Java.

Why interface is used for multiple inheritance?

As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.


2 Answers

A good example for an interface is a repository pattern. Your interface will define methods like Get, GetAll, Update, Delete, etc. No implementation, just function signatures.

Then, you can write a 'concrete' implementation of that class to work with, say, MySQL. Your UI should only refer to the interface, though.

Later, if you decide to change to Microsoft SQL, you write another concrete implementation, but your UI code doesn't have to change (much).

Multiple inheritance doesn't exist in C#, in the sense that you can only inherit from one 'concrete' class; though you can inherit (or 'implement') as many interfaces as you want.

like image 105
Matthew Groves Avatar answered Sep 20 '22 08:09

Matthew Groves


I am writing a video game. In this video game I apply different forces to objects in the game. Thrust forces, impact forces, gravitational forces. While they are calculated differently, they all have the same basic elements. I need to call an update function that will evaluate the force and add the force to the object it's attached to.

So, what I've done is create an IForce interface that has an update function for its signature. All of my forces implement this interface:

public interface IForce
{
    void Update(Particle particle, GameTime gameTime);
}

Here is a sample implementation.

public class Spring : IForce
{
    private Particle ThisParticle;
    private Particle ThatParticle;

    private float K;

    public Spring(Particle thisParticle, Particle thatParticle, float k)
    {
        ThisParticle = thisParticle;
        ThatParticle = thatParticle;
    }

    public void Update(Particle particle, GameTime gameTime)
    {            
        float X = Vector3.Length(ThisParticle - ThatParticle);

        ThisParticle.Forces.Add(K * X);
    }
}

The update function has a simplified spring force update to make it easier to understand.

This helps in a few ways.

I can completely change the way a force is calculated without effecting other parts of my code. I do this all the time. Along the same lines, it is rediculously easy for me to add new forces. As long as it implements the IForce interface I know it will mesh well with my existing code.

Another way it helps is with handling a large number of forces. I have a force registry that has a List of IForce. Since all forces implement that interface and have an Update function it's very easy to update all the forces in my game. When I create the force I add it to the list. Then, I loop through the list and call each elements update function without worrying about what type of force it is and all my forces update.

I use interfaces every day in a lot of different situations. They are fantastic!

like image 23
Eric Avatar answered Sep 19 '22 08:09

Eric