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

A text from a book which must be wrong(virtual, abstract metod)

Hello!

I'm reading in a book about C# and here is something that sound strange.

"You can override a virtual method (or accessor) with an abstract method (or
abstract accessor) in a derived class.Any classes derived from this abstract
class must override these abstract methods and provide new implamantations
for them to become non-abstract."

What override a virtual method (or accessor) with an abstract method ??
This must be wrong??

//Tony
Nov 25 '07 #1
2 1187
Tony Johansson wrote:
Hello!

I'm reading in a book about C# and here is something that sound strange.

"You can override a virtual method (or accessor) with an abstract method (or
abstract accessor) in a derived class.Any classes derived from this abstract
class must override these abstract methods and provide new implamantations
for them to become non-abstract."

What override a virtual method (or accessor) with an abstract method ??
This must be wrong??
It is possible to do so, but in my experience not widely used.

One scenario I can come up with is that you have a base class that you
wish to descend from, but you wish to force descendants that implement
functionality to replace a method that the base class have provided a
default implementation for.

Example:

public class LoggerBase
{
public virtual void LogMessage(String s)
{
Debug.WriteLine(s);
}
}

public class YourNewLoggerBase : LoggerBase
{
public abstract override void LogMessage(String s);
}

public class SomeNewLoggingClass : YourNewLoggerBase
{
public override void LogMessage(String s)
{
//
}
}

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
Nov 25 '07 #2
Tony Johansson <jo*****************@telia.comwrote:
I'm reading in a book about C# and here is something that sound strange.

"You can override a virtual method (or accessor) with an abstract method (or
abstract accessor) in a derived class.Any classes derived from this abstract
class must override these abstract methods and provide new implamantations
for them to become non-abstract."

What override a virtual method (or accessor) with an abstract method ??
This must be wrong??
It's easy enough to show that it's correct:

public class Base
{
public virtual void Foo()
{
}
}

public abstract class Derived : Base
{
public abstract override void Foo();
}

Basically it's saying, "Derived is rejecting the implementation of Foo
in Base - it's not optional to override it in classes derived from
Derived, you *have* to override it."

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 25 '07 #3

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

Similar topics

4
by: wesley | last post by:
Hi, Why must abstract classes implements all the methods/property in the interface it implements? Since it's an abstract class it shouldn't be able to be instantiated and the child classes are...
62
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
12
by: placid | last post by:
Hi if a have the following classes class A { public: A(); virtual ~A(); virtual string someFunction() const = 0;
0
by: Vasco Lohrenscheit | last post by:
Hello, in the following situation the wrong virtual method gets called: ----------------------------------------------------- //assembly1 #include "../Plugin/iPlugin.h" //ISO c++ type iPlugin...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
7
by: v4vijayakumar | last post by:
Is it possible to implement member object's virtual functions, in the containing class? If not, is it possible to simulate this behavior? ex: class test { protected: virtual void fun() = 0;...
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;"...
0
by: mradam | last post by:
Good morning. Could someone please assist me in find the correct way to do this. I have tried a couple methods but it seems VS 2008 does not support all the directives that previous versions did, or...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.