473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C# example

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
Nov 15 '05 #1
4 1475

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

Nov 15 '05 #2
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
Nov 15 '05 #3
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());

}

"ichor" <al**@hotmail.com> wrote in message
news:eX**************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #4
ichor... Usage

Class1 c1= new Class1();
Class1 c2= new Class2();

System.Console.WriteLine(c1.method());
System.Console.WriteLine(c2.method(0);

http://www.geocities.com/jeff_louie/OOP/oop8.htm

Regards,
Jeff
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.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
5
by: relaxedrob | last post by:
Hi All! I ran the following w3c example through my WSDL parser (SOA Editor from Cape Clear): http://www.w3.org/TR/wsdl#_rpcexample It told me that there were a bunch of erros about no...
11
by: ajikoe | last post by:
Hello, I used Visual C# Standard Edition. I want to comment my program using xml commentary method, I don't know why if I use value and example tag, it is not working / showed in the html...
6
by: cj | last post by:
Lets just take this example I'm looking at now. I'm looking at the help screen titled .NET Framework Class Library FolderBrowserDialog Class . It gives an example at the bottom that begins with:...
6
by: Guy Macon | last post by:
While I agree with the sentiment, the oringinal title on this thread ("OT: Specially for , why you should always use example.com for obfuscating domains") is wrong. There are other reserved domain...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.