> For the complete documentation index, see [llms.txt](https://chihaolu.gitbook.io/all-in-one-solidity/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chihaolu.gitbook.io/all-in-one-solidity/part-ii-medium/chapter-11-ji-cheng-inheritance/inheritance.md).

# 【Inheritance】

繼承是一個能夠使用其他 contract 的一種方法。

派生（子）合約：可以訪問所有非私有成員，包括變量和內部方法，但不允許從合約內不使用他

```solidity
// First import the contract
import B from 'path/to/B.sol';

//Then make your contract inherit from it
contract A is B {

  //Then call the constructor of the B contract
  constructor() B() {}
}
```

如果我們同時宣告一樣的函數在不同 contract：

```solidity
contract B {
   function foo() external {...}
}
contract A is B {
   function foo() external {...}
}
```

當我們 call 了 `foo()` 在 A 這個 contract 裡面，則屬於 A 的 `A.foo()` 會被執行。

但要注意以下這種情況屬於不同的函數

```solidity
contract B {
   function foo(uint data) external {...}
}
contract A is B {
   function foo() external {...}
}
```

當我們呼叫了 `foo(1)` 在 A 這個 contract 裡面，則 `B.foo()` 會被執行，因為只有 B 的 `foo(uint)` 有宣告 uint。

所以要注意的是所謂的「一模一樣的函數」，是要連 `returns`、參數都一模一樣！
