473,230 Members | 1,376 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,230 software developers and data experts.

about abstract class and override

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 keyword abstract.
I have also another class called MyDerivedClass which is a sublass to
MyClass.

Now to my question when I implement this method myTest in class
MyDerivedClass I must use
the keyword override. I just wonder I have always used this override when
having a virtual method with the same signature in the base class. Can
somebody explain the reson for this using of override?

public interface ITest
{
void myTest();
}

abstract public class MyClass : ITest
{
abstract public void myTest;
...
}

public class MyDerivedClass : MyClass
{
public override void myTest()
{
}
}

//Tony

Jun 27 '08 #1
5 2195
Tony Johansson <jo*****************@telia.comwrote:
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 keyword abstract.
I have also another class called MyDerivedClass which is a sublass to
MyClass.

Now to my question when I implement this method myTest in class
MyDerivedClass I must use
the keyword override. I just wonder I have always used this override when
having a virtual method with the same signature in the base class. Can
somebody explain the reson for this using of override?
myTest *is* a virtual method - just one with no implementation.

Abstract impllies virtual - the method needs to be looked up at
execution time, because there's no way of knowing which implementation
will be used based on the base class reference.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
Still kind of weird that you have to use the “override” keyword on
abstract method do you think?

I mean, abstract methods have no implementation whatsoever, they are
just like interfaces but you don’t implement an interface method by
using the “override” keyword.

So the question is. Why force using the override keyword on abstract
methods? Why not just treat this abstract method like interface
methods and use the same syntax when implementing the method on the
derived class?

Do I think this is a big deal? No, I honestly don’t really care how
you have to implement this, I am simply curious.

Cheers

On Jun 6, 2:17*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Tony Johansson <johansson.anders...@telia.comwrote:
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 keyword abstract.
I have also another class called MyDerivedClass which is a sublass to
MyClass.
Now to my question when I implement this method myTest in class
MyDerivedClass I must use
the keyword override. I just wonder I have always used this override when
having a virtual method with the same signature in the base class. Can
somebody explain the reson for this using of override?

myTest *is* a virtual method - just one with no implementation.

Abstract impllies virtual - the method needs to be looked up at
execution time, because there's no way of knowing which implementation
will be used based on the base class reference.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Jun 27 '08 #3
qg**********@mailinator.com wrote:
Still kind of weird that you have to use the “override” keyword on
abstract method do you think?

I mean, abstract methods have no implementation whatsoever, they are
just like interfaces but you don’t implement an interface method by
using the “override” keyword.

So the question is. Why force using the override keyword on abstract
methods? Why not just treat this abstract method like interface
methods and use the same syntax when implementing the method on the
derived class?
Why should the simple system that if you override a virtual function
you must use "override", have a different syntax for virtual functions
that are overridden, but appear to be abstract in the base class?

What if you refactor your code and decide to put some code in the
originally abstract function after all?
--
Rudy Velthuis http://rvelthuis.de

"Those are my principles. If you don't like them I have others."
-- Groucho Marx
Jun 27 '08 #4
<qg**********@mailinator.comwrote:
Still kind of weird that you have to use the =3Foverride=3F keyword on
abstract method do you think?
Absolutely not. It's a definitely good thing - it means you don't
accidentally provide an implementation for an abstract method. Consider
this:

Version 1:
public abstract class Base
{
public abstract void FirstMethod();
}

public class Derived : Base
{
public override void FirstMethod() { /* Implementation */ }

public void SecondMethod() { /* Implementation */ }
}
Version 2:

public abstract class Base
{
public abstract void FirstMethod();
public abstract void SecondMethod();
}

public class Derived : Base
{
public override void FirstMethod() { /* Implementation */ }

public void SecondMethod() { /* Implementation */ }
}

Note how Base now has a method called SecondMethod. Derived should
*not* compile now. It shouldn't silently be implementing an abstract
method without the developer realising - the contract for
Base.SecondMethod might be *completely* different to the meaning of
Derived.SecondMethod.
I mean, abstract methods have no implementation whatsoever, they are
just like interfaces but you don=3Ft implement an interface method by
using the =3Foverride=3F keyword.
This is true, but perhaps unfortunate. I suspect people are a little
bit more wary of introducing new methods in interfaces than they are of
introducing new abstract methods in classes. Maybe.
So the question is. Why force using the override keyword on abstract
methods? Why not just treat this abstract method like interface
methods and use the same syntax when implementing the method on the
derived class?

Do I think this is a big deal? No, I honestly don=3Ft really care how
you have to implement this, I am simply curious.
Given the above, I think it's interface implementation which is
"wrong" here, rather than abstract method implementation. :)

Hope that's given you a little something to think about - it certainly
has for me :)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #5
No question, I agree with you and Rudy, I rather be forced to use
override than not since it provides me with a layer of protection in
case I indivertibly do something I didn’t mean to do.

The only thing that bugs me a little is the word “override” since you
are not really overriding anything since there is nothing to override
on an abstract method/property (they are empty and you can’t do things
like calling base.xxxxx etc from them).

Maybe a keyword such as “Implement” would be nicer. Then you could use
that keyword when implementing abstract member as well as interface
members and make the purpose of your method official that way.

Anyway, I have to admit, a lot of the times I just post just to get
your reaction, its always interesting to see what you have to say.

Cheers.
Rene
On Jun 6, 3:29*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
*<qglyirnyf...@mailinator.comwrote:
Still kind of weird that you have to use the =3Foverride=3F keyword on
abstract method do you think?

Absolutely not. It's a definitely good thing - it means you don't
accidentally provide an implementation for an abstract method. Consider
this:

Version 1:
public abstract class Base
{
* * public abstract void FirstMethod();

}

public class Derived : Base
{
* * public override void FirstMethod() { /* Implementation */ }

* * public void SecondMethod() { /* Implementation */ }

}

Version 2:

public abstract class Base
{
* * public abstract void FirstMethod();
* * public abstract void SecondMethod();

}

public class Derived : Base
{
* * public override void FirstMethod() { /* Implementation */ }

* * public void SecondMethod() { /* Implementation */ }

}

Note how Base now has a method called SecondMethod. Derived should
*not* compile now. It shouldn't silently be implementing an abstract
method without the developer realising - the contract for
Base.SecondMethod might be *completely* different to the meaning of
Derived.SecondMethod.
I mean, abstract methods have no implementation whatsoever, they are
just like interfaces but you don=3Ft implement an interface method by
using the =3Foverride=3F keyword.

This is true, but perhaps unfortunate. I suspect people are a little
bit more wary of introducing new methods in interfaces than they are of
introducing new abstract methods in classes. Maybe.
So the question is. Why force using the override keyword on abstract
methods? Why not just treat this abstract method like interface
methods and use the same syntax when implementing the method on the
derived class?
Do I think this is a big deal? No, I honestly don=3Ft really care how
you have to implement this, I am simply curious.

Given the above, I think it's interface implementation which is
"wrong" here, rather than abstract method implementation. :)

Hope that's given you a little something to think about - it certainly
has for me :)

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Jun 27 '08 #6

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

Similar topics

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...
15
by: Tee | last post by:
Hi, I have a base usercontrol with a method (blank method, no code), I have another few usercontrols that will inherit this base usercontrol, but I want to force all the usercontrol that...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
8
by: Claire | last post by:
I have a base abstract class A which contains several abstract functions I then have an abstract class B (derived from A) which overrides a small subset of A's abstract functions Finally I...
9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
7
by: erin.sebastian | last post by:
Hello Everyone, This is my first attempt at coding using an abstract class and i am getting an error i can't figure out. Here is the back ground. I have a project that contains an abstract...
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
4
by: David Zha0 | last post by:
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...
4
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. The question is about this method GetCows() {...} which is a member in the generic...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.