473,320 Members | 2,193 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,320 software developers and data experts.

About virtual and abstract method

Hi,

"when we call a virtual method, the runtime will check the instance who
called the method and then choose the suitable override method, this may
causes the performance drop down", is this right?

And, why not use "new" instead of using "virtual"?

And the last question, what is the differences between a abstract method and
a interface?

Thanks.

Apr 12 '07 #1
4 6536

"David Zha0" <zh*****@163.comwrote in message
news:OC**************@TK2MSFTNGP05.phx.gbl...
Hi,

"when we call a virtual method, the runtime will check the instance who
called the method and then choose the suitable override method, this may
causes the performance drop down", is this right?
Yes, because the exact behavior of the call can be different for each
object. Although the JIT compiler may be able to inline virtual calls
anyway for the most common cases. This is really only a concern for short
methods, where the callvirt overhead is a large fraction of total execution
time.
>
And, why not use "new" instead of using "virtual"?
New creates a new method slot, so you don't override the existing method.
That is to say, that a call made through a base class (or interface)-typed
variable won't call your version.
>
And the last question, what is the differences between a abstract method
and
a interface?
An abstract method can exist in a class alongside non-abstract methods. For
instance:

abstract class Checksum32Computer
{
public abstract bool Calc(byte[] array, int offset, int length, out int
checksum);
public bool Calc(byte[] array, out int checksum) { return Calc(array, 0,
array.Length, out checksum); }
}

But you only get single inheritance of abstract classes.
Apr 12 '07 #2
Hi David,
"when we call a virtual method, the runtime will check the instance who
called the method and then choose the suitable override method, this may
causes the performance drop down", is this right?
Yes, but that doesn't matter much in most cases.
And, why not use "new" instead of using "virtual"?
because you *want* the program to make that decission at runtime.
e.g. you could have a general Product class with several derived classes.
The Product class could have a method CalculatePrice and every derived class
can implement its own pricing schema.

Then, if this code calls CalculatePrice on an exporession of type Product,
the right procing schema is called automatically. The caller doesn't have to
know wich implementation to call, but the object itself knows.
That's OOP.
And the last question, what is the differences between a abstract method
and
a interface?
An abstract method is simply one class member with no implemetation. Other
members of the same type may not be abstract. An interface, in the other
hand is a type without any implementation.
Also interfaces support multiple inheritance.

HTH
Christof
Apr 12 '07 #3
On 12 Apr, 13:40, "David Zha0" <zhpl...@163.comwrote:
Hi,

"when we call a virtual method, the runtime will check the instance who
called the method and then choose the suitable override method, this may
causes the performance drop down", is this right?

And, why not use "new" instead of using "virtual"?

And the last question, what is the differences between a abstract method and
a interface?

Thanks.
Hi,

I'll work backwards as it will make more sense that way:

An interface is commonly considered a contract. If you have an
interface:

public interface AnInterface
{
void foo2();
void foo3();
}

Any class that implements that interface must provide an
implementation for all the elements (methods etc). All those methods
will be public (within the scope of the class of course)

public class ImplementationTest : AnInterface
{
public void foo()
{
}
void AnInterface.foo2()
{
}
}

Notice the foo is declared as public and foo2 is prefixed with the
interface name and a dot?
If we instantiate ImplementationTest you will be able to use foo
immediately, but not foo2. To get to foo2 you would need to cast to
the interface or assign it to an interface variable as follows.

ementationTest();
AnInterface second = first;
first.foo();
//first.foo2(); - can't see it!
second.foo(); // same as doing first.foo();
second.foo2(); //now we can see it!
Ok, so that's interfaces, lets look at abstract classes. An abstract
class is meant to be a template for a class. You can't instantiate
them, you can derive a class from it though and assign any derived
type to the a variable of the abstract class.

public abstract class AbstractClass
{
public abstract void fii();
protected abstract void fii2();
}

The first thing you can see is we have a public and protected method,
you can't do that with an interface. Here's a class that inherits from
Abstract class and does something.

public class Derived1 : AbstractClass
{
public override void fii()
{
Console.WriteLine("1.fii");
}
protected override void fii2()
{
Console.WriteLine("1.fii2");
}
}

To use we instantiate it and can optionally assign it to an
AbstractClass variable. It makes no odds at this point, but will
shortly.

Derived1 d1 = new Derived1();
AbstractClass a1 = d1;
a1.fii();
d1.fii();
Abstract classes can also implement interfaces and contain actual
working code. Lets add to the above:

public abstract class AbstractClass : AnInterface
{
public abstract void fii();
protected abstract void fii2();
public abstract void foo();
void AnInterface.foo2()
{
}
public void PublicMethod()
{
Console.WriteLine("PublicMethod");
}
}

First you'll notice foo and foo2 from our interface earlier. foo2 will
look familiar, foo however now has the abstract keyword meaning we can
provide an implementation of it in our derived class. foo2 will not
appear in our derived class.
Next notice PublicMethod. It's not abstract and not virtual. Don't
worry about virtual, we'll get to that shortly. As it isn't abstract.
Lets see it in action.

Derived1 d1 = new Derived1();
AbstractClass a1 = d1;
AnInterface i1 = d1;
d1.PublicMethod(); // works! Implementation is in AbstractClass
i1.foo2(); //works! implementation is in AbstractClass
d1.foo(); //works! implementation in Derived1

So as you can see our derived class now implements AnInterface but
only contains half the code, sharing the work with AbstractClass.
AbstractClass is also providing PublicMethod free of choice. If we
write Derived2,3,4,5, they will all get access to PublicMethod.

Now lets look at virtual and show another facet of derived classes
(whether from an abstract or non abstract base class).

public class AClass
{
public virtual void fee()
{
Console.WriteLine("2.fee");
}
public virtual void fee2()
{
Console.WriteLine("2.fee2");
}
}

virtual simply means if I derive a new class from this one I may
override it. This means that when you mark a method as abstract you
are also saying it is virtual.

public class AClass2 : AClass
{
public override void fee()
{
Console.WriteLine("AClass2.fee");
base.fee ();
}
public new void fee2()
{
Console.WriteLine("AClass2.fee2");
base.fee2();
}
}

And some test code.

AClass a1 = new AClass();
AClass2 a2 = new AClass2();
AClass a3 = a2;

Lets look at the output to see what the differences are. To recap,
AClass is the base class, AClass2 is the derived class. fee is
overriden, fee2 is overriden with the new keyword instead. Incidently
the derived methods call the base class's implementation of the method
after doing their own work. That's what base.method() does. So AClass2
is calling code in AClass.

Here's the test code with what gets printed.

a1.fee(); //fee on base class
a1.fee2(); //fee2 on base class

AClass.fee
AClass.fee2

So as expected it simply prints whats in the methods.
----

a2.fee(); //derived class
a2.fee2(); //derived class

AClass2.fee
AClass.fee
AClass2.fee2
AClass.fee2

More interesting, they print their message and then call AClass so we
get two lines of output for each line of code.
----

a3.fee(); //Base class variable holding reference to derived class
a3.fee2(); //Base class variable holding reference to derived class

AClass2.fee
AClass.fee
AClass.fee2

Three lines? fee was overriden with the override keyword, this means
it overrides the base class even if it's a base class variable.
fee2 was overriden with the new keyword, so when we give an AClass
variable a reference to an AClass2 instance AClass is free to use it's
own implementation.

Hope that helps.

Apr 12 '07 #4
Thanks a lot for your reply, they are very helpful.

Have a nice day, thank you again.

"David Zha0" <zh*****@163.comдÈëÏûÏ¢ÐÂÎÅ:OC**************@TK2M SFTNGP05.phx.gbl...
Hi,

"when we call a virtual method, the runtime will check the instance who
called the method and then choose the suitable override method, this may
causes the performance drop down", is this right?

And, why not use "new" instead of using "virtual"?

And the last question, what is the differences between an abstract method
and
an interface?

Thanks.

Apr 13 '07 #5

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

Similar topics

15
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance...
51
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
7
by: Vadim Berezniker | last post by:
From what I read, the only similiar thing in C# is declaring a method as abstract. Problem is I wanted to only declare some methods as "pure". So I obviously cannot use an abstract modifier in this...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
2
by: Dan Holmes | last post by:
Suppose i have this class declaration: public abstract class ConfigurableComponent : Component, IConfigure if IConfigure has a method with this signature: ...
3
by: chandu | last post by:
hello, what is the difference to use the keyword virtual,abstract when we are overriding the methods.instead of abstract shall we use virtual everywhere when we need to override? i am little...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.