C# example

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: ichor (ally_at_hotmail.com)
Date: 02/26/04


Date: Thu, 26 Feb 2004 12:52:46 +1100

hi i am trying to understand this para from the mcad textbook

When a member is overridden, the new member is called in place of the member
in the base class. This is true regardless of the context in which the
member is called.

this is what i want to create an example of.

"For example, if an instance of an inherited class is cast to its base class
and an overridden method is called, the new implementation will be executed,
even though the variable is of the base class type. The type of the object,
not the variable, determines which member is called. "

I created 2 classes and would like to call the "method" to depict the para
above.

public class Class1
 {
  public Class1()
  {

  }

  public virtual string method()
  {
       String s = "calls old method";
       return s;
  }
 }

 public class Class2: Class1
 {
  public Class2()
  {

  }

  public override string method()
  {
       String s = "calls new method";
       return s;

  }
 }

but i dont know how to call the "method" so that i can understand what this
para says..

any help would be great..

thanx



Relevant Pages

  • Overriding interface members
    ... And I want to override ... Declaring the member with override, ... my interface member completely replaces the member ... from the base class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: diff between virtual and abstract?
    ... meaningful body in the base class, as it can't be defined), then it should ... if you just want to give the subclass the ... *ability* to override a method then it should be virtual. ... abstract member forces the entire class to be abstract, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# example
    ... > When a member is overridden, the new member is called in place of the ... > even though the variable is of the base class type. ... > public override string method() ... > para says.. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can my class tell if one of its virtual members is overloaded?
    ... > that a base class cannot discover anything about derived ... The point is that you can never know what all the derived classes of a ... isn't another type around which *does* override a member. ...
    (microsoft.public.dotnet.languages.csharp)
  • member overridding
    ... I have a base class with a static member function. ... I tried overridding my static member in a simple test class derived ...
    (alt.comp.lang.learn.c-cpp)