Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is-A vs Has-A relationship

Tags:

c#

Newbie Question which my course instructor has effectively confused the tar out of me. Is-A relationship is when say a Dog is a animal. Then you'd make Animal be the Base class with Dog as a derivative class.

But for our Class Project I have to make a class based on an interface. With a separate class which only has one method to give a random number. Seemingly they aren't really related, but I have to call the randomNumber Method from the main class.

His question during the instructions (which are terrible) say to figure out if this is an IS-A or HAS-A relationship. Based on the facts I'd say this is a HAS-A relationship, but I have no idea how to reference the class, but to make a variable assigned to the class so I can use it.

Is there another way to Reference the class which I don't know?

Devices randomMeasure = new Devices();  //Random measurement Class
this.mostRecentMeasure = randomMeasure.GetMeasurement();
like image 398
Crazyd Avatar asked Oct 29 '13 19:10

Crazyd


4 Answers

If your class inherits from the other class, it is an "IS-A" relationship.

If the class is passed to the other class in the constructor this is a "HAS-A" relationship.

e.g.

public class foo : bar
{
    // IS-A
}



public interface IBar
{
}

public class Bar : IBar
{
}

public class Foo : IBar
{
    private Bar _bar;

    public foo(Bar bar)
    {
        _bar = bar;

    }

    // HAS-A
}

However, it's not obvious what it is your've asking. In the second case, Foo IS-A IBar and HAS-A Bar.

like image 75
BanksySan Avatar answered Oct 20 '22 15:10

BanksySan


An Interface is a Can-Do relationship.
You can assign an instance of a class to any variable that is of the type of

  1. the class itself or
  2. one of the base classes of the class or
  3. an interface that the class or one of its base classes implement.

In your example (case 3), this means:

IDevice randomMeasure = new Devices();
randomMeasure.GetMeasurement();

This concept is referred to as polymorphism.

like image 27
Markus Avatar answered Oct 20 '22 14:10

Markus


This is a IS-A relationship, the

To reference it, you should create an instance of the concrete class and assign it to the variable of the interface type:

interface IMeasureable 
{
    public int GetMeasurement();
}
class Device : IMeasureable 
{
    public int GetMeasurement() 
    {
        return .... 
    }
}
class App 
{
    public void Main() 
    {
         IMeasureable thing = new Device();
         int x = thing.GetMeasurement();
    }
}
like image 3
OscarRyz Avatar answered Oct 20 '22 16:10

OscarRyz


To answer your question ( although I am not sure why did you ask) You may

  1. Instantiate the class

  2. Make it static

  3. Keep the class object in a application variable / Cache / View State etc

  4. Use extension method which will give you random number ( You dont have to use another class in that case)

like image 2
Pratap Das Avatar answered Oct 20 '22 14:10

Pratap Das