Solidity has the keywords abstract and virtual. What is the difference?

1 Answer 1


The abstract and virtual keywords signify that the associated code will be implemented or overridden elsewhere in the code. The main difference is that abstract applies to contracts and virtual applies to functions.

The purpose of the abstract keyword is to decouple the definition of a contract from its implementation providing better extensibility and self-documentation and facilitating helpful patterns. Abstract contracts are useful in the same way that defining methods in an interface is useful. It is a way for the designer of the abstract contract to say “any child of mine must implement this method”.

The purpose of the virtual keyword is to be more explicit when overriding a function. Base functions can be overridden by inheriting contracts to change their behavior if they are marked as virtual. The overriding function must then use the override keyword in the function header. You can read more details about virtual and override here.

Abstract Example (From Solidity Docs)

pragma solidity ^0.6.0;

abstract contract Feline {
    function utterance() public virtual returns (bytes32);

contract Cat is Feline {
    function utterance() public override returns (bytes32) { return "miaow"; }

Virtual Example (From Solidity Docs)

pragma solidity >=0.5.0 <0.7.0;

contract Base {
    function foo() virtual public {}

contract Middle is Base {}

contract Inherited is Middle {
    function foo() public override {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.