www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Error when implementing methods of abstract class inherited from

reply "Uranuz" <neuranuz gmail.com> writes:
I don't even know if this is a bug or a feature of language. I 
will just put it here.

interface ITaskDoer
{
	int doTask1();
	string doTask2(string someName);
	
	bool doTask3(bool value);
	
}

abstract //Do abstract makes interface methods abstract?
class BaseTaskDoer: ITaskDoer
{
	override string doTask2(string someName)
	{	return "You have passed name: \"" ~ someName ~ "\"";
	}
	
	//Uncommenting this makes code working,
	//but this is not much pretty looking code
	//override {
	//	abstract int doTask1();
	//	abstract bool doTask3(bool value);
	//}
}

class TaskDoer: BaseTaskDoer
{
	override int doTask1()
	{	return 100; }
	
	override bool doTask3(bool value)
	{	return !value; }
	
}

void main()
{	import std.stdio;
	auto doer = new TaskDoer;
  	writeln(doer.doTask1()," ",doer.doTask2("John")," 
",doer.doTask3(true));
	
}

Compilation output:
/d124/f170.d(27): Error: function f170.TaskDoer.doTask1 does not 
override any function, did you mean to override 
'f170.ITaskDoer.doTask1'?
/d124/f170.d(30): Error: function f170.TaskDoer.doTask3 does not 
override any function, did you mean to override 
'f170.ITaskDoer.doTask3'?

Please tell me the right way deal with this case? Is it a bug or 
not?
Oct 30 2013
parent reply "Uranuz" <neuranuz gmail.com> writes:
This example may be tested here:
http://dpaste.dzfl.pl/2ecdd48a
Oct 30 2013
parent reply "rumbu" <rumbu rumbu.ro> writes:
On Wednesday, 30 October 2013 at 10:17:17 UTC, Uranuz wrote:
 This example may be tested here:
 http://dpaste.dzfl.pl/2ecdd48a
Since BaseTaskDoer *must* implement ITaskDoer, the correct implementation is: class BaseTaskDoer: ITaskDoer { string doTask2(string someName) { return "You have passed name: \"" ~ someName ~ "\""; } abstract int doTask1(); abstract bool doTask3(bool value); }
Oct 30 2013
parent reply "Uranuz" <neuranuz gmail.com> writes:
I think that I understand it now. If class A implements some 
interface B it should implement all methods of interface B even 
if they are abstract.
Oct 30 2013
parent "Dicebot" <public dicebot.lv> writes:
On Wednesday, 30 October 2013 at 17:14:39 UTC, Uranuz wrote:
 I think that I understand it now. If class A implements some 
 interface B it should implement all methods of interface B even 
 if they are abstract.
Yes. Making abstract class itself can be useful if you have implemented all its methods but still want it to be used only for inheritance as making instances of abstract classes is compile-time error. If there is at least on abstract method it does not make any difference (other than documenting intention)
Oct 30 2013