3

I am currently studying on multiple inheriting operation.What ihave learnt is we can't inherit multiple class but we can inherit a class and an interface so i just need to switch a class to an interface. Like below

class a{}
class b{}
interface d{}
//class c:a,b{} That does not work
//class c:a,d{} or class c:b,d{} but these need to work

If i return to my question respect for multiple inheritance operation, Here is an complete program that explains my problem.

using System;
using System.Collections.Generic;
using System.Text;

namespace interface_ile_multiple_inheritance
{
  interface set_get
  {
    double _getx
    { get; }

    double _gety
    { get ; }

    protected double total;//I want to use it by inheriting
  }

  class set_items
  {
    protected double x;
    protected double y;
    public set_items(double _x, double _y)
    {
        x = _x;
        y = _y:
     }
  }

  class display : set_items, set_get
  {
    public display(double x, double y) : base(x, y) { }

    public  double _getx
    { get { return x; } }

    public double _gety
    { get { return y; } }

    public double total_res()
    {
        total = x + y;    // Error line 1 total does not exist
        return total;     //Error line 2 total does not exist
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
        display disp1 = new display(12, 12);

        Console.WriteLine("Total:" + disp1.total_res().ToString());//HERE İ NEED TO FİND x+y
        Console.WriteLine("Area:" + (disp1._getx * disp1._gety).ToString());//HERE İ NEED TO FİND x*y
    }
  }
}

Even though i can inherit interface's property signatures,why can't i inherit its integer variable?

  • 4
    Because variables are part of an implementation, not an interface. Interfaces don't hold state of any kind; for that, you need a class. – Robert Harvey Nov 16 '17 at 4:58
  • When you define properties in an interface, you define than the class must implements those properties. Which mean you're supposed to have a total field in your subclass. This is specially usefull if you have abstract elements that must provide the same kind of events for instance. – Walfrat Nov 16 '17 at 8:21
  • 1
    @Sipo Well, the question is why he can't inherit from an interface with a variable, when he should realize that he can't define an interface with a variable in the first place. – Sebastian Redl Nov 16 '17 at 8:35
  • @SebastianRedl, having a misconception and asking a question based on that misconception doesn't make the question bad. – Sipo Nov 16 '17 at 8:40
  • This question should not be on hold. Even though the example is code, the idea behind it is about OOP. This should be in Stack Overflow. – Sipo Nov 16 '17 at 14:54
2

I have no C# experience but I think this is a general OOP problem. I am looking at this question from a Java standpoint.

First some terminology:

interfaces implement what has to be done. When you use an interface you speak of implementing it.

classes implement how it has to be done. When you use a class to inherit from, you speak of inheriting it.

That is why you can add values and write proper methods in classes to inherit, but not in interfaces.


However this doesn't mean that you can not define variables in an interface. You can, but there are two problems with what you are trying:

1.)

The defined variables are going to be similar to constants which could make the purpose of your variable useless.

In Java you can call them statically. Interface.variable

2.)

The second problem in your case should be the protected status of your total variable.

Protected is pretty much a private variable with an exception for classes that inherit it. But remember that interfaces are not inherited, so the variable is pretty much private.

Of what use would this variable be as a private?


My suggestion:

Think about if total is really part of the interface or if it doesn't fits better into the exact implementation or the inherited class.

1

Let's say you have a television. On the front of it, there are an "on/off" switch, "channel up" and "channel down" buttons, "volume up" and "volume down" buttons, and some other buttons. Those buttons are for the user to handle the television.

Inside the television there are a bunch of other buttons to set the complex settings of the television. Those settings and the buttons to handle those settings should not concern the user, they are for the television technician to make sure the television works as it should.

This distinction between buttons for the user and buttons for the technician is the core distinction between private methods/variables, and public methods/variables.

The only reason we have a television is for the user to use it, and the only way the user can use it is through the buttons on the front of it. In the same way - the only reason we have a class is for other classes the use it, and the only way other classes can use it is through the methods and variables that are available to them, the public ones.

The only reason we have buttons for the technician inside the television is to help the buttons on the front to work properly. In the same way - the only reason we have private methods and variables is to help the public ones work properly.

We have a name for the public methods and variables of a class. We call it "API". In the television analogy, the API is the collection of settings and buttons that are available to the user. An API is all the class components that are available to other classes.

Now, let's say you have an application on your smartphone that turns on and off all the electrical devices in your house. The application can turn on and off only devices with an "On/Off" switch. Devices like a microwave, cannot be turned on and off from the application because they need to be manually disconnected from the power supply. So the application needs to make sure the devices do have an "On/Off" switch. In other words, we need to make sure that all the devices we want to turn on or off has an API for it.

That is what an interface is.

An interface is a way of making sure that several classes have a common API.

So let's write an actual C# interface:

interface IHaveOnOffSwitch
{
    void TurnOn();
    void TurnOff();
}

We could have three classes: TV, Radio, and Microwave. TV and Radio have an "On/Off" switch, so they implement the interface:

class TV : IHaveOnOffSwitch
{
    private TvType type; // a random private variable

    void TurnOn()
    {
        // logic for turnning on
    }
    void TurnOff()
    {
        // logic for turning off
    }
}
class Radio : IHaveOnOffSwitch
{
    private RadioType type; // a random private variable 

    void TurnOn()
    {
        // logic for turnning on
    }
    void TurnOff()
    {
        // logic for turning off
    }
}

class Microwave
{

}

And now the smartphone application methods could be written like this:

void TurnDeviceOn(IHaveOnOffSwitch device)
{
    device.TurnOn();
}
void TurnDeviceOff(IHaveOnOffSwitch device)
{
    device.TurnOff();
}

The reason the application uses the interface is to know that the devices have this API, it doesn't care what are the private variables of the device (like the television type or the radio type).

That is why an interface cannot have private methods and variables. It is all about what the class allows other classes to do with it, and not about how the class deal with it internally.

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