C# example

I

ichor

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
 
G

Guest

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


When a member is overridden, the new member is called in place of the membe
in the base class. This is true regardless of the context in which th
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 clas
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 par
above

public class Class

public Class1(




public virtual string method(

String s = "calls old method"
return s



public class Class2: Class

public Class2(




public override string method(

String s = "calls new method"
return s




public class MainClas

public MainClass(



Class2 obj1 = new Class2()
string s = obj.method()
System.console.writeline(s)

Class1 obj2 = (Class1) obj
string s = obj2
System.console.writeline(s)


I hope this help..



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

any help would be great.

than
 
G

Guest

In your example try this..

static void Main( string[] args

Class1 class1 = new Class1()
Console.Out.WriteLine( class1.method() )
Class2 class2 = new Class2()
Console.Out.WriteLine( ((Class1)class2).method() )
Class1 class3 = new Class2()
Console.Out.WriteLine( class3.method() )


The point is... whatever the type of the object (right side of the new operator), that method will be invoked

good luck with the test...

~harri
http://www.harrisreynolds.net/weblog
 
M

Malek

simple example using your classes:

Class1 c = new Class2();

MessageBox.Show(c.method());

more illustrative (polymorphism) example :

public class Form1 : System.Windows.Form

//declarations and generation code for the Form and the two buttons

private void Button1_Click(object sender, EventArgs e)

{

Class1 c1 = new Class1();

callMethod(c1);

}

private void Button2_Click(object sender, EventArgs e)

{

Class2 c2 = new Class2();

callMethod(c2);

}

public void callMethod(Class1 c)

{

MessageBox.Show(c.method());

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top