understanding Interfaces

Hitoruna

Member
Joined
Sep 12, 2017
Messages
11
Programming Experience
5-10
Hello. This is my first post here. I am a programmer with C and C++ experience, and lately I have been heavily programming in C#. I suppose it is quite common I don't use all features at once so, for the first time I am dealing with Interfaces. I would very much appreciate if someone can help me with understanding the following

I have an interface
C#:
public interface ITracker    {        
    void CreateTargetModel(......);
        void Track(.....);
        Bitmap ProcessedImage { get;}
        Dictionary<string, string> Information { get;}        
    }

and a class that implements it
C#:
public class Centroid: ITracker
{

}

I have this concept that interfaces are like abstract classes and can not be instantiated but in another class I have
C#:
public class anotherClass
{
ITracker _tracker =null;

private ITracker Tracker
{
  get{
   if(_tracker==null) _tracker= new TrackerFactory(TrackerType.CENTROID).Tracker;
   return _tracker;
     }
}

private void TrackObject()
{
  Tracker.Track((Bitmap) image, .....);
}

private void CreateTarget()
{
 Tracker.CreateTargetModel(....);
}
}

So my questions are:

1) Even though Centroid implementes ITracker , the class anotherClass has as its member variable a ITracker _tracker. Why? How is this possible?
I have the suspicion that this is so that the member _tracker can be of class Centroid or any other class that also implements ITracker.
But if this is so, why not just use a base class?

2) I get that we use a propery (Tracker) to get a new _tracker. This property instantiate a Centroid it seems using a Tracker factory.
When is this property called??
(This is related with my next question)
3) Originally _tracker is null. When I do Tracker.CreateTargetModel() or Tracker.Track() am i calling the propertiy Tracker's get?? everytime?

My apologies for the beginner questions. I have been trying to understand a program that uses much more features that I am accustomed to, and i would really like to learn.

thanks in advance
 
I have the suspicion that this is so that the member _tracker can be of class Centroid or any other class that also implements ITracker.
But if this is so, why not just use a base class?
A class can only inherit a single base class, but it can implement many interfaces. Different kind of unrelated classes can implement any interface, they can have just that particular functionality in common.
I get that we use a propery (Tracker) to get a new _tracker. This property instantiate a Centroid it seems using a Tracker factory.
Any method of "anotherClass" that uses its private Tracker property will create an object (that implementes ITracker) and assign the field.
When I do Tracker.CreateTargetModel() or Tracker.Track() am i calling the propertiy Tracker's get?? everytime?
Yes.
 
Last edited:
I think it is only the "Rate this thread" or star icon on post to add rep to a user. You should also be able to edit original thread post and set thread prefix to for example Answered/Resolved.
 
To hide the original class in other assemblies or classes, its recommended to use the Interface for the type reference. In your example, if "AnotherClass" was in a separate assembly, by only referencing the Interface you could avoid direct references to the original concrete class; like putting the Interfaces into their own assembly.

Another thing to note, would be the use of casting to different levels of the inheritance chain. If you did have access to the concrete type, in your example you could cast the Interface 'ITracker' to the concrete type 'Centroid' to gain access to the additional non-interface methods and properties.

Ben
https://www.youtube.com/watch?v=O9VSEHSN9k8
 
Back
Top Bottom