locked
Difference between... RRS feed

  • Question

  • I have a question. Please find the code below:
    class MyClass11 : outclass, IInterface
    {
    
    
    }
    class MyClass1 : outclass, IInterface
    {
    
    }
    
    interface IInterface
    {
    
    }
    
    class outclass
    {
    
    }
    
    public void ConstructCollection()
     {
    
    
      List<outclass> sa = new List<outclass>(); 
      List<IInterface> isa = new List<IInterface>();
    
      outclass temp = new MyClass1(); //Line1
      IInterface temp1 = new MyClass1(); //Line2
    
      MyClass1 mc = new MyClass1();//Line3
    
    
      sa.Add(temp);
      isa.Add(temp1);
      
     }
    
    

     

    In the above code what is the difference between Line1, Line2 and Line3 ? All three instantiate objects of Class MyClass1() if i am not wrong. Can you Kindly Clarify on this ?


    Known is a drop, Unknown is an Ocean
    Thursday, July 15, 2010 4:19 PM

Answers

  • Even i can do like this right ?

    I would like to know when will i use an interface and when will i use a class for this kind of implementation. Hope i didn't confuse too much.
    Known is a drop, Unknown is an Ocean

    That is a big question and there is no easy answer.  To really get your head round classes vs interfaces try reading one of these...

    (The 1st one is quite dry so I would maybe go for the O'reilly book 1st).

     

    Good luck

    • Proposed as answer by StLuisRey Thursday, July 15, 2010 7:39 PM
    • Marked as answer by MDM13 Monday, July 19, 2010 5:50 PM
    Thursday, July 15, 2010 7:39 PM
  • Here is a sample that will illustrate what casting does.  It can act as a filter on an object.  Set a breakpoint where I indicated, and watch what happens as that object is constructed.  You will see the base class object get constructed, followed by the derived class object. 

    When you cast a derived class to a base class, it is that initial object that you see constructed that gets "filtered" out of the object graph in memory.  The entire object is still fully intact in memory, but only a portion of it is accessible because of the cast.

      class Class1
      {
        public void ConstructCollection()
        {
          List<outclass> sa = new List<outclass>();
          List<IInterface> isa = new List<IInterface>();
    
          outclass temp = new MyClass1();  //Line1
          IInterface temp1 = new MyClass1(); //Line2
          MyClass1 mc = new MyClass1();   //Line3: Breakpoint
    
          sa.Add(temp);
          isa.Add(temp1);
          //
          // type the following lines, no cut and paste!
          //
          temp.OutclassMethod();
          temp1.InterfaceMethod();
          mc.MyClass1Method();
    
        }
      }
    
      class MyClass11 : outclass, IInterface
      {
        int num = 0;
        public MyClass11()
        {
          return; // give Debugger something to show you
        }
        public void InterfaceMethod()
        {
          return;
        }
        internal void MyClass11Method()
        {
          return;
        }
      }
      
      class MyClass1 : outclass, IInterface
      {
        int num = 0;
        public MyClass1()
        {
          return; // give Debugger something to show you
        }
        public void InterfaceMethod()
        {
          return;
        }
        internal void MyClass1Method()
        {
          return;
        }
      }
    
      interface IInterface
      {
        void InterfaceMethod();
      }
    
      class outclass
      {
        int num1 = 0;
        public outclass()
        {
          return; // give Debugger something to show you
        }
        public void OutclassMethod()
        {
        }
      }
    
    

    Be sure to type those lines out that I indicated.  You will see only those portions of the object that are defined by the type that the object was cast to.

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by MDM13 Monday, July 19, 2010 5:48 PM
    Thursday, July 15, 2010 11:11 PM
    Moderator

All replies

  • You are right - they all instanciate objects of type MyClass1.  What you need to keep in mind is that MyClass1 is also of type outclass and of IInterface at the same time and you can cast between them.  I guess your question really is why you can/want this behavior?

    It is easier to explain if we take a more concrete example.  Say we had a set of things that were printable and each of those objects knew how to print itself we might define an interface called IPrintable and have a class for each type i.e. PdfDocument, JpgImage etc implement the IPrintable interface.

    Now, say we want to implement a print queue (like your List<IInterface>) we can add printable things to our print queue, read the list and call the print() method on each of the objects without caring about the underlying type.  The point being that the print queue doesn't need to know/care what it is printing - a PdfDocument would look the same as a JpgImage since they are both of type IPrintable.

    Hope that help.

    Thursday, July 15, 2010 5:25 PM
  • The difference is what you can do with the objects, and compile time type checking.

    //sa.Add(temp1); // will not compile, since temp1 is of type IInterface.

    sa.Add((MyClass1)temp1); compiles and works

     

    Similarly, if you had

    class outclass {

    public void PrintOutclass() {}

    }

    .

    .

    .

    // temp1.PrintOutclass(); // will not compile, since temp1 is of type IInterface

    ((MyClass1)temp1).PrintOutclass(); // compiles and works as expected.

     

    Of course, you should really be using the "as" or "is" operator and checking for null;

     

    Thursday, July 15, 2010 5:37 PM
  • Even i can do like this right ?

    sa.Add((outclass)temp1);
    
    isa.Add((MyClass1)temp1);
    
    As you can see both can be casted to their appropriate types and then added. I can understand the intend based on why we need a design like this based on StLuisRey's answer. I would like to know when will i use an interface and when will i use a class for this kind of implementation. Hope i didn't confuse too much.
    Known is a drop, Unknown is an Ocean
    Thursday, July 15, 2010 6:38 PM
  • Even i can do like this right ?

    I would like to know when will i use an interface and when will i use a class for this kind of implementation. Hope i didn't confuse too much.
    Known is a drop, Unknown is an Ocean

    That is a big question and there is no easy answer.  To really get your head round classes vs interfaces try reading one of these...

    (The 1st one is quite dry so I would maybe go for the O'reilly book 1st).

     

    Good luck

    • Proposed as answer by StLuisRey Thursday, July 15, 2010 7:39 PM
    • Marked as answer by MDM13 Monday, July 19, 2010 5:50 PM
    Thursday, July 15, 2010 7:39 PM
  • Here is a sample that will illustrate what casting does.  It can act as a filter on an object.  Set a breakpoint where I indicated, and watch what happens as that object is constructed.  You will see the base class object get constructed, followed by the derived class object. 

    When you cast a derived class to a base class, it is that initial object that you see constructed that gets "filtered" out of the object graph in memory.  The entire object is still fully intact in memory, but only a portion of it is accessible because of the cast.

      class Class1
      {
        public void ConstructCollection()
        {
          List<outclass> sa = new List<outclass>();
          List<IInterface> isa = new List<IInterface>();
    
          outclass temp = new MyClass1();  //Line1
          IInterface temp1 = new MyClass1(); //Line2
          MyClass1 mc = new MyClass1();   //Line3: Breakpoint
    
          sa.Add(temp);
          isa.Add(temp1);
          //
          // type the following lines, no cut and paste!
          //
          temp.OutclassMethod();
          temp1.InterfaceMethod();
          mc.MyClass1Method();
    
        }
      }
    
      class MyClass11 : outclass, IInterface
      {
        int num = 0;
        public MyClass11()
        {
          return; // give Debugger something to show you
        }
        public void InterfaceMethod()
        {
          return;
        }
        internal void MyClass11Method()
        {
          return;
        }
      }
      
      class MyClass1 : outclass, IInterface
      {
        int num = 0;
        public MyClass1()
        {
          return; // give Debugger something to show you
        }
        public void InterfaceMethod()
        {
          return;
        }
        internal void MyClass1Method()
        {
          return;
        }
      }
    
      interface IInterface
      {
        void InterfaceMethod();
      }
    
      class outclass
      {
        int num1 = 0;
        public outclass()
        {
          return; // give Debugger something to show you
        }
        public void OutclassMethod()
        {
        }
      }
    
    

    Be sure to type those lines out that I indicated.  You will see only those portions of the object that are defined by the type that the object was cast to.

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by MDM13 Monday, July 19, 2010 5:48 PM
    Thursday, July 15, 2010 11:11 PM
    Moderator