1

I have several questions regarding contract abstraction

contract A {
    address owner;
    uint randomVariable;

    function A(uint _randomVariable) {
        owner = msg.sender;
        randomVariable = _randomVariable;
    }
}
contract B is A {
    address owner;
    uint randomVariable2;

    function B(uint _randomVariable2) {
        owner = msg.sender;
        randomVariable2 = _randomVariable2;
    }

    function changeOwner(address _newOwner) {
        owner = _newOwner;
    }
}

1 how does this exactly work? In the end A and B are the same Contract called B right? Because they get deployed at the same address right?

2 How are the variables treated in this contract? we have two variables called owner which get set to the same address. is this the same variable? what happens if we change the owner in contract B

3 how does the call of the constructor behave? When I create the contract, do I call the constructor seperately or is it merged and I have to pass the first and second random variable?

1

What you are inquiring about is called inheritance.

When you have contract B inherit from contract A (contract B is A) you are telling B to have not only it's own variables and functions defined, but also those in contract A.

If you deploy contract B (which inherits from contract A) you are NOT deploying 2 contracts you are deploying just 1 contract which has it's variables and functions "merged" with the contracts it inherits from.

An instance of contract B will have both B's and A's variables. Remember, it is only 1 contract that exists. If you also deployed a contract A they would be separate instances that are not related at all between each other (other than sharing a similar structure).

When dealing with functions, if you implement on B a function with the same signature (same name, paramters, return types) as one of A's, then function B implementation overrides A's. So, calling someFunc() on this instace will do whatever B defined and not A, if they have the same signature.

When instantiating B, it's constructor will be called. If you also want to have A's constructor called, you have to do it explicitly.

In your example above:

B's constructor should look like:

function B(uint _randomVariable2, uint _randomVariableA) 
A(_randomVariableA){
        owner = msg.sender;
        randomVariable2 = _randomVariable2;
    }

This will cause A's constructor to be called, passing the parameter/s you specify.

When you do B contractB = new B(20,30); B will call A's constructor and set this new instance's _randomVariable to 30 and then B's constructor will be called setting randomVariable2 to 20. Also, owner will be set to msg.sender twice.

  • so address owner in A gets overwritten right? – Daniel Gretzke Dec 10 '17 at 17:37
  • also what is the added value of using abstract contracts? – Daniel Gretzke Dec 10 '17 at 17:38
  • Don’t confuse abstraction with inheritance. Inheritance (what you are asking about here) allows you to avoid code duplication, for example. You could have a contract with certain variables and functions and then several other contracts that inherit from that and implement their own additional variables/functions. You can put all the common elements in the base contract so you don’t have to repeat code. – pabloruiz55 Dec 10 '17 at 17:41
  • thank you for your help! whats contract abstraction then? – Daniel Gretzke Dec 10 '17 at 19:01

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.