In Solidity, inheritance allows child contracts to reuse parent contract code while customizing specific functions. To enable function customization, parent contracts must declare functions as 'virtual', and child contracts must use the 'override' keyword when redefining those functions. Functions not overridden in the child contract remain accessible from the parent, enabling multiple levels of inheritance where each level can override functions from its parent.
Solidity Inheritance: Virtual & Override Keywords Explained
Added:let's take a look at inheritance and solidity let's say that i have a contract a with some code in it and then i also have contract b where i want to use most of the code in contract a but maybe customize a few functions inside contract b one way to do it is copy all of the code inside contract a paste it into contract b and let's say that i wanted to customize the function foo in bar so what i would do is maybe change this to b and also change this code a little bit there's a lot of duplicate code between contract b and contract a however using inheritance will be able to remove a lot of the duplicate code for this example contract b will inherit from contract a so contract a will be the parent and contract b will be the child contract that we want to customize to start off with the first thing that we need to do is declare the functions that's going to be inherited as virtual for this example we want to customize foo in bar inside contract b so these are the two functions that will be inherited by contract b and to declare that this function can be customized by child contract we need to put in a keyword called virtual this keyword virtual will tell solidity that this function can be inherited and customized by the chart contract we also want the function bar to be customizable by a child contract so again here we'll put the keyword virtual next we want to declare that contract b is going to inherit from contract a it's going to inherit all of the code and we want to be able to customize fluent bar so to do that we first say b is a and for the function full and bar these are the functions that we want to customize from contract a we declare as override same with bar we declare as over right to show the demo of this inheritance of foo and bar i've also created a function called baz inside contract a since contract b inherits contract a inside here although the function is not declared this function will be available let me show you so i'm going to compile the contract and then deploy contract b scroll down open contract b and notice that we have three functions here bar bads and food but inside contract b we only declare two function foo and bar and not bass so where does bass come from well best comes from contract a we declared it over here and contract b inherits contract a so that is why contract b has the function bas even though we have not declared it here when we call the function foo we get the string b likewise when we call the function bar we also get the string b however when we call the function baz we get the string a flu and bar are functions that we inherit from contract a and then beta customized it in contract b and the function pass remains the same let me give you a little bit more complex example now let's say that we wanted a contract that inherits from contract b and we wanted to override the bar function how do we do it the first thing that we want to do is declare that bar can be overwritten by the child contract and to do that you'll have to declare as virtual and for contract c we'll declare that it inherits from contract b by saying contract c is b and then we'll override this function by copy paste and then say the function bar overrides the function bar of contract b and for the string will return c compile the contract deploy contract c and again we have three functions foo bar and bass foo returns b bar returns c and badge returns a
Up Next

Reentrancy Attack in Solidity: Smart Contract Security Explained
@JohnnyTime
4.4K views•2022-08-25

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Arbitrage Algorithm for Uniswap V2 and V3 Pools
@smartcontractprogrammer
166 views•2026-02-18

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto




![Understanding classes and object-oriented programming [Python Tutorial]](https://i.ytimg.com/vi/_vr5faCXFo8/maxresdefault.jpg)





![SOLIDITY – БАЗОВЫЙ КУРС [Урок 3]](https://i.ytimg.com/vi/EYZUabLpgUc/maxresdefault.jpg)


![Ultimate Intro to Ethereum Ðapp Development [Part 11] - Contract Inheritance](https://i.ytimg.com/vi/rY2JxxzIYb0/hqdefault.jpg)



![Visibility Specifiers [Create Blockchain Applications Using Solidity]](https://i.ytimg.com/vi/zHVugSpasmI/maxresdefault.jpg)











![10.TypeScript Classes | Inheritance | Method Overriding| Static Keyword [16-Mar-2021]](https://i.ytimg.com/vi/NUnanG1XTVg/hqdefault.jpg)







