Please Help: question about overriding class methods via new 
Author Message
 Please Help: question about overriding class methods via new

Hello. i've got a question on this code:

sing System;

interface IStorable
{
   void Read(  );
   void Write(  );

Quote:
}

// Simplify Document to implement only IStorable
public class Document : IStorable
{
   // the document constructor
   public Document(string s)
   {
      Console.WriteLine(
         "Creating document with: {0}", s);

   }

   // Make read virtual
   public virtual void Read(  )
   {
      Console.WriteLine(
         "Document Read Method for IStorable");
   }

   // NB: Not virtual!
   public void Write(  )
   {
      Console.WriteLine(
         "Document Write Method for IStorable");
   }

Quote:
}

// Derive from Document
public class Note : Document
{
   public Note(string s):
      base(s)
   {
      Console.WriteLine(
         "Creating note with: {0}", s);
   }

   // override the Read method
   public override void Read(  )
   {
      Console.WriteLine(
         "Overriding the Read method for Note!");
   }

   // implement my own Write method
   public new void Write(  )
   {
      Console.WriteLine(
         "Implementing the Write method for Note!");
   }

Quote:
}

public class Tester
{

   static void Main(  )
   {
      Note note2 = new Note("Second Test");
      IStorable isNote2 = note2 as IStorable;
      if (isNote != null)
      {
         isNote2.Read(  );
         isNote2.Write(  );
      }

      Console.WriteLine("\n");

    }

Quote:
}

the question is when isNote2.Write() is
invoked, "Document::Write()" is invoked and
not "Note::Write()". I was expecting that since Note2
which is of type Note is cast to IStorable, Note::Write()
would be invoked. Could you please explain why is it
otherwise? thanks.


Thu, 07 Oct 2004 04:56:36 GMT  
 Please Help: question about overriding class methods via new
When you cast to an interface, you get the Document implementation (because
Document implements the interface and Note doesn't).

You'll get the behavior you want if you have Note implement IStorable too...

--
Visit the C# product team at http://www.gotdotnet.com/team/csharp

This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
> Hello. i've got a question on this code:

> sing System;

> interface IStorable
> {
>    void Read(  );
>    void Write(  );
> }

> // Simplify Document to implement only IStorable
> public class Document : IStorable
> {
>    // the document constructor
>    public Document(string s)
>    {
>       Console.WriteLine(
>          "Creating document with: {0}", s);

>    }

>    // Make read virtual
>    public virtual void Read(  )
>    {
>       Console.WriteLine(
>          "Document Read Method for IStorable");
>    }

>    // NB: Not virtual!
>    public void Write(  )
>    {
>       Console.WriteLine(
>          "Document Write Method for IStorable");
>    }

> }

> // Derive from Document
> public class Note : Document
> {
>    public Note(string s):
>       base(s)
>    {
>       Console.WriteLine(
>          "Creating note with: {0}", s);
>    }

>    // override the Read method
>    public override void Read(  )
>    {
>       Console.WriteLine(
>          "Overriding the Read method for Note!");
>    }

>    // implement my own Write method
>    public new void Write(  )
>    {
>       Console.WriteLine(
>          "Implementing the Write method for Note!");
>    }
> }
> public class Tester
> {

>    static void Main(  )
>    {
>       Note note2 = new Note("Second Test");
>       IStorable isNote2 = note2 as IStorable;
>       if (isNote != null)
>       {
>          isNote2.Read(  );
>          isNote2.Write(  );
>       }

>       Console.WriteLine("\n");

>     }
> }

> the question is when isNote2.Write() is
> invoked, "Document::Write()" is invoked and
> not "Note::Write()". I was expecting that since Note2
> which is of type Note is cast to IStorable, Note::Write()
> would be invoked. Could you please explain why is it
> otherwise? thanks.



Sat, 09 Oct 2004 00:24:44 GMT  
 Please Help: question about overriding class methods via new
Hello Eric. thanks for your reply.
it's true that Document class implements the IStorage
interface. but according to the code below, the
class "Note" is a derived class of the class Document.
Since we've instantiated an object of type "Note" and cast
it to the IStorable interface, i would deduce that
the "Write" implementation of class "Note" would be called.

we have class Document:IStorable then class Note:Document.
but i agree with you that if we had class Note:IStorable
then, undoubtedly, Note::Write would be called. So my
question is what happens if we have the case:
class Note:Document, as stated in the code below, and we
have this code:
      Note note2 = new Note("Second Test");
      IStorable isNote2 = note2 as IStorable;
      if (isNote != null)
         isNote2.Write(  );

should'nt "isNote2.Write(  );" invoke Note::Write()
instead of Document::Write() since we have
class Note:Document? thanks.

interface IStorable
{
   void Read(  );
   void Write(  );

Quote:
}

// Simplify Document to implement only IStorable
public class Document : IStorable
{
   // the document constructor
   public Document(string s)
   {
      Console.WriteLine(
         "Creating document with: {0}", s);

   }

   // Make read virtual
   public virtual void Read(  )
   {
      Console.WriteLine(
         "Document Read Method for IStorable");
   }

   // NB: Not virtual!
   public void Write(  )
   {
      Console.WriteLine(
         "Document Write Method for IStorable");
   }

Quote:
}

// Derive from Document
public class Note : Document
{
   public Note(string s):
      base(s)
   {
      Console.WriteLine(
         "Creating note with: {0}", s);
   }

   // override the Read method
   public override void Read(  )
   {
      Console.WriteLine(
         "Overriding the Read method for Note!");
   }

   // implement my own Write method
   public new void Write(  )
   {
      Console.WriteLine(
         "Implementing the Write method for Note!");
   }

Quote:
}

public class Tester
{

   static void Main(  )
   {
      Note note2 = new Note("Second Test");
      IStorable isNote2 = note2 as IStorable;
      if (isNote != null)
      {
         isNote2.Read(  );
         isNote2.Write(  );
      }

      Console.WriteLine("\n");

    }

Quote:
}



Sat, 09 Oct 2004 10:08:21 GMT  
 Please Help: question about overriding class methods via new
I think the right way to think about this is that interfaces are implemented
directly by objects, and don't participate in virtual dispatch.

If you want to cast an object to an interface, you'll need to implement that
interface directly to get the behavior you want.

Of course, you could have another virtual function that Document defines and
override that in your derived class.

--
Visit the C# product team at http://www.gotdotnet.com/team/csharp

This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
> Hello Eric. thanks for your reply.
> it's true that Document class implements the IStorage
> interface. but according to the code below, the
> class "Note" is a derived class of the class Document.
> Since we've instantiated an object of type "Note" and cast
> it to the IStorable interface, i would deduce that
> the "Write" implementation of class "Note" would be called.

> we have class Document:IStorable then class Note:Document.
> but i agree with you that if we had class Note:IStorable
> then, undoubtedly, Note::Write would be called. So my
> question is what happens if we have the case:
> class Note:Document, as stated in the code below, and we
> have this code:
>       Note note2 = new Note("Second Test");
>       IStorable isNote2 = note2 as IStorable;
>       if (isNote != null)
>          isNote2.Write(  );

> should'nt "isNote2.Write(  );" invoke Note::Write()
> instead of Document::Write() since we have
> class Note:Document? thanks.

> interface IStorable
> {
>    void Read(  );
>    void Write(  );
> }

> // Simplify Document to implement only IStorable
> public class Document : IStorable
> {
>    // the document constructor
>    public Document(string s)
>    {
>       Console.WriteLine(
>          "Creating document with: {0}", s);

>    }

>    // Make read virtual
>    public virtual void Read(  )
>    {
>       Console.WriteLine(
>          "Document Read Method for IStorable");
>    }

>    // NB: Not virtual!
>    public void Write(  )
>    {
>       Console.WriteLine(
>          "Document Write Method for IStorable");
>    }

> }

> // Derive from Document
> public class Note : Document
> {
>    public Note(string s):
>       base(s)
>    {
>       Console.WriteLine(
>          "Creating note with: {0}", s);
>    }

>    // override the Read method
>    public override void Read(  )
>    {
>       Console.WriteLine(
>          "Overriding the Read method for Note!");
>    }

>    // implement my own Write method
>    public new void Write(  )
>    {
>       Console.WriteLine(
>          "Implementing the Write method for Note!");
>    }
> }
> public class Tester
> {

>    static void Main(  )
>    {
>       Note note2 = new Note("Second Test");
>       IStorable isNote2 = note2 as IStorable;
>       if (isNote != null)
>       {
>          isNote2.Read(  );
>          isNote2.Write(  );
>       }

>       Console.WriteLine("\n");

>     }
> }



Sun, 10 Oct 2004 00:16:02 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to Stop Derived Class from overriding base class method

2. Howto Expose a normal C++ class with methods via an Interface Class in ATL COM

3. new keyword and method overriding

4. Overriding method in COM class

5. Prevent overriding method in abstract base class ?

6. Override methods for class defined in MFC DLL

7. Override methods for class defined in MFC DLL

8. Inheritance: overriding base-class methods

9. Call methods of dynamically loaded classes via interface?

10. override method question

11. Question about overriding methods

12. Overriding methods in subclass question...

 

 
Powered by phpBB® Forum Software