BabyDoge inherits from Doge. That means if you compile and deploy BabyDoge, it will have access to both catchphrase()and anotherCatchphrase() (and any other public functions we may define on Doge).
This can be used for logical inheritance (such as with a subclass, a Cat is an Animal). But it can also be used simply for organizing your code by grouping similar logic together into different contracts.
Example when constructor arguments are required
// SPDX-License-Identifier: MITpragmasolidity ^0.8.18;import"./FundMe.sol";contractFundMeMatchingisFundMe {// Type declarationsusingPriceConverterforuint256; // Extends uint256 (used from msg.value) to enable direct price conversion/** * This is how to create a constructor for an inherited contract * if the parent already has a constructor that has arguments passed * https://docs.soliditylang.org/en/develop/contracts.html#arguments-for-base-constructors */constructor(address priceFeedAddress) FundMe(priceFeedAddress) {}functionfund() publicpayableoverride {if (msg.value.getConversionRate(s_priceFeed) <= MINIMUM_USD)revertFundMe__NotEnoughEthSent(); s_addressToAmountFunded[msg.sender] += msg.value; s_funders.push(msg.sender); }}
// SPDX-License-Identifier: MITpragmasolidity ^0.8.18;/* Graph of inheritance A / \ B C / \ /F D,E*/contract A {functionfoo() publicpurevirtualreturns (stringmemory) {return"A"; }}// Contracts inherit other contracts by using the keyword 'is'.contractBisA {// Override A.foo()functionfoo() publicpurevirtualoverridereturns (stringmemory) {return"B"; }}contractCisA {// Override A.foo()functionfoo() publicpurevirtualoverridereturns (stringmemory) {return"C"; }}// Contracts can inherit from multiple parent contracts.// When a function is called that is defined multiple times in// different contracts, parent contracts are searched from// right to left, and in depth-first manner.contractDisB, C {// D.foo() returns "C"// since C is the right most parent contract with function foo()functionfoo() publicpureoverride(B,C) returns (stringmemory) {return super.foo(); }}contractEisC, B {// E.foo() returns "B"// since B is the right most parent contract with function foo()functionfoo() publicpureoverride(C,B) returns (stringmemory) {return super.foo(); }}// Inheritance must be ordered from โmost base-likeโ to โmost derivedโ.// Swapping the order of A and B will throw a compilation error.contractFisA, B {functionfoo() publicpureoverride(A,B) returns (stringmemory) {return super.foo(); }}
Inherited State Variables
Unlike functions, state variables cannot be overridden by re-declaring it in the child contract.
The only way it can be overridden is by setting it directly in the construction of the child contract.
// SPDX-License-Identifier: MITpragmasolidity ^0.8.18;contract A {stringpublic name ="Contract A";functiongetName() publicviewreturns (stringmemory) {return name; }}// Shadowing is disallowed in Solidity 0.6// This will not compile// contract B is A {// string public name = "Contract B";// }contractCisA {// This is the correct way to override inherited state variables.constructor() { name ="Contract C"; }// C.getName returns "Contract C"}