1

I've seen similar questions, but none answer why people use abstract contracts. I come from a non-OOP JavaScript background, so I have some trouble understanding these things.

From the docs:

pragma solidity ^0.4.0;
contract Feline {
    function utterance() public returns (bytes32);
}
contract Cat is Feline {
    function utterance() public returns (bytes32) { return "miaow"; }
}

What is the benefit of defining Feline here in the first place? The only benefit I can think of is if you have many different kind of feline contracts and you want to make sure they all include utterance() I guess? But I really cannot think of any such situations...

Are there benefits I'm not seeing? Coming from my background you'd want to avoid things like this, where you just want a Banana but get a Gorilla holding it, along with the entire Jungle...

PS: The question is not the difference between interfaces and abstract contracts.

1

You are not forced to create and abstract contract to do an implementation. Solidity is not a strongly OOP language, even if something is derived from OOP (interface, inheritance, abstract contracts).

So said abstract contracts will give you another level of abstraction and the main reason to use it is to be sure that who implements the contract will follow its definition (as you described in your post) and provide common functionalities to the child contracts.

You can find plenty post on stackoverflow that will describe the benefits. Here and here for example.

From one of those posts:

Abstract Classes are a good fit if you want to provide implementation details to your children but don't want to allow an instance of your class to be directly instantiated (which allows you to partially define a class).
Abstract methods are useful in the same way that defining methods in an Interface is useful. It's a way for the designer of the Abstract class to say "any child of mine MUST implement this method".

| improve this answer | |
  • Cool! I didn't check stackoverflow! What do you mean with "to be sure that who implements the contract" How/why would someone try to implement my contract? – mesqueeb May 9 '18 at 10:02
  • Is not someone else but you. If you have different Felines you want them to follow the same structure. Then you define the abstract contract and all your child contracts will follow it. There are cases when the an abstract contract can be a standard de-facto (look at the ERC20 token) but that's another story ( theethereum.wiki/w/index.php/ERC20_Token_Standard ) – mirg May 9 '18 at 10:15
  • 2020 update: there's a new abstract keyword that can be placed in front of the contract definition when defining an abstract. – Paul Razvan Berg Jun 6 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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