www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - TDPL 6.4.3 Overriding Is Only Voluntary

reply Caligo <iteronvexor gmail.com> writes:
"The override keyword in the signature of Friend.bgColor is required," (193)

This code compiles:

class Contact{ string bgColor(){ return ""; } }
class Friend : Contact{
    string bgColor(){ return "LightGreen"; }
}

So how is 'override' required?  Can I get an example of where
'override' makes a difference?
Mar 22 2011
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Caligo Wrote:

 "The override keyword in the signature of Friend.bgColor is required," (193)
 
 This code compiles:
 
 class Contact{ string bgColor(){ return ""; } }
 class Friend : Contact{
     string bgColor(){ return "LightGreen"; }
 }
 
 So how is 'override' required?  Can I get an example of where
 'override' makes a difference?

This is probably due to compiler not enforcing the rule. There is no difference from having the 'override' and not having it. What it provides is documentation that the function comes from the parent and it will throw an error if you are not overriding a function. As this documentation is so useful it is considered as something that should just be required even though the compiler knows what is being overridden. C# also allows you to create a 'new' function, which doesn't override but is the same name and signature, I don't know its importance.
Mar 23 2011