Learn Solidity - Build Decentralized Application in Ethereum

Learn Solidity: Inheriting Smart Contracts

In this post, we will learn how to inherit & extend existing smart contracts in Solidity Language. [pastacode lang=”javascript” manual=”pragma%20solidity%200.4.8%3B%0A%0A%2F%2F%20Solidity%20supports%20multiple%20inheritance%20by%20copying%20code%20including%20polymorphism.%0A%0A%2F%2F%20All%20function%20calls%20are%20virtual%2C%20which%20means%20that%20the%20most%20derived%20function%20is%20called%2C%20%0A%2F%2F%20except%20when%20the%20contract%20name%20is%20explicitly%20given.%0A%0A%2F%2F%20Even%20if%20a%20contract%20inherits%20from%20multiple%20other%20contracts%2C%20only%20a%20single%20contract%20%0A%2F%2F%20is%20created%20on%20the%20blockchain%2C%20the%20code%20from%20the%20base%20contracts%20is%20always%20copied%20into%20the%20final%20contract.%0A%0A%2F%2F%20In%20general%20inheritance%20system%20in%20solidity%20is%20very%20similar%20to%20Python%E2%80%99s%2C%20%0A%2F%2F%20especially%20concerning%20multiple%20inheritance.%0A%0A%2F%2F%20Lets%20checkout%20one%20example%0A%0A%2F%2F%20We%20will%20create%20a%20simple%20base%20class%20first%0Acontract%20owned%20%7B%0A%20%20%20%20function%20owned()%20%7B%20owner%20%3D%20msg.sender%3B%20%7D%0A%20%20%20%20address%20owner%3B%0A%7D%0A%0A%2F%2F%20Now%20we%20can%20extende%20the%20owned%20using%20is%20keyword%20like%20this%0Acontract%20mortal%20is%20owned%20%7B%0A%0A%09%2F%2F%20here%20kill%20will%20have%20the%20access%20of%20owner%20the%20variable%20%0A%09%2F%2F%20which%20is%20available%20in%20the%20owned%20contract%0A%09%2F%2F%20please%20note%20that%20owned%20contract’s%20constructor%20will%20be%20%0A%09%2F%2F%20executed%20at%20the%20time%20of%20mortal’s%20initialization%0A%20%20%20%20function%20kill()%20%7B%0A%20%20%20%20%20%20%20%20selfdestruct(owner)%3B%0A%20%20%20%20%7D%0A%7D%0A%0A%2F%2F%20Multiple%20inheritance%20is%20also%20possible.%20Note%20that%20%22owned%22%20is%0A%2F%2F%20also%20a%20base%20class%20of%20%22mortal%22.%0A%2F%2F%20in%20such%20cases%20the%20order%20of%20inheritance%20is%20important%20to%20avoid%20the%20%0A%2F%2F%20Diamond%20problem%20(which%20is%20know%20in%20multiple%20inheritance)%0A%2F%2F%20Let’s%20take%20an%20example.%0A%0A%0Acontract%20User%20is%20mortal%2C%20owned%20%7B%0A%0A%20%20%20%20string%20public%20UserName%3B%0A%0A%20%20%20%20function%20User(string%20_name)%7B%0A%20%20%20%20%20%20%20%20UserName%20%3D%20_name%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F%2F%20here%20User%20inherits%20both%20the%20cntracts%20owned%20%26%20mortal%20which%20is%20resulting%20in%20diamond%20problem%0A%20%20%20%20%2F%2F%20Changing%20the%20order%20of%20inheritance%20might%20resolve%20the%20issue%20of%20this%20diamond%20problem.%0A%20%20%20%20%2F%2F%20The%20reason%20for%20this%20is%20that%20%22User%22%20requests%20%22owned%22%20to%20override%20%0A%20%20%20%20%2F%2F%20%22mortal%22%20(by%20specifying%20mortal%2C%20owned%20in%20this%20order)%2C%20but%20%22mortal%22%20itself%20%0A%20%20%20%20%2F%2F%20requests%20to%20override%20%22owned%22%2C%20which%20is%20a%20contradiction%20that%20cannot%20be%20resolved.%0A%20%20%20%20%2F%2F%20Here%20reverting%20the%20order%20will%20solve%20this%20problem%0A%20%20%20%20%2F%2F%20A%20simple%20rule%20to%20remember%20is%20to%20specify%20the%20base%20classes%20%0A%20%20%20%20%2F%2F%20in%20the%20order%20from%20%E2%80%9Cmost%20base-like%E2%80%9D%20to%20%E2%80%9Cmost%20derived%E2%80%9D.%0A%7D%0A%0A%0A%0A” message=”” highlight=”” provider=”manual”/]  
Read More