0% found this document useful (0 votes)
4 views9 pages

Types of Inheritance

The document outlines various types of inheritance in Solidity, including single, multiple, hierarchical, and multilevel inheritance. Each type is explained with examples of contracts demonstrating how derived contracts can inherit properties and methods from base contracts. The document serves as an educational resource for understanding inheritance in blockchain programming for the course on Blockchain in the academic year 2024-25.

Uploaded by

rupadhyay7405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Types of Inheritance

The document outlines various types of inheritance in Solidity, including single, multiple, hierarchical, and multilevel inheritance. Each type is explained with examples of contracts demonstrating how derived contracts can inherit properties and methods from base contracts. The document serves as an educational resource for understanding inheritance in blockchain programming for the course on Blockchain in the academic year 2024-25.

Uploaded by

rupadhyay7405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Semester: - VII Subject: Blockchain AY: 2024-25

Types of Inheritance

Single inheritance:
In single inheritance, a derived contract inherits from only one base contract.
contract A { /* Base contract */ }
contract B is A { /* Derived contract inheriting from A */ }
Use Case: When you want to extend or modify the behavior of a single contract.

Multiple Inheritance:
In multiple inheritance, a derived contract inherits from more than one base contract.
contract A { /* Base contract */ }
contract B { /* Another base contract */ }
contract C is A, B { /* Derived contract inheriting from both A and B */ }
Use Case: When you want to combine functionalities from multiple base contracts into
one derived contract.

Hierarchical Inheritance:
In hierarchical inheritance, multiple derived contracts inherit from a single base contract.
contract A { /* Base contract */ }
contract B is A { /* Derived contract 1 */ }
contract C is A { /* Derived contract 2 */ }

Use Case: When you want to create different contracts that share common functionality

Prof. Ramya.R.B Department of Computer Engineering, APSIT


Semester: - VII Subject: Blockchain AY: 2024-25

from a single base contract.

Multilevel Inheritance:
In multilevel inheritance, a derived contract inherits from another derived contract,
forming a chain.
contract A { /* Base contract */ }
contract B is A { /* Derived contract inheriting from A */ }
contract C is B { /* Another derived contract inheriting from B (and transitively from A)
*/ }
Use Case: When you want to build a series of contracts where each one builds on the
functionality of the previous one.

Single Inheritance

This Solidity program demonstrates the concept of single inheritance using two contracts: Person and
Student. The Person contract serves as the base contract, containing basic attributes like name and
age, and a function getDetails() to retrieve these details. The Student contract is derived from Person,
inheriting its properties and functions. Additionally, Student introduces a new attribute, school, specific
to the student and provides a function getSchool() to retrieve the school name. The Student contract's
constructor calls the Person constructor using the Person(_name, _age) syntax to initialize the
inherited attributes. This demonstrates how the Student contract extends the Person contract while
adding its own specific functionality.

Prof. Ramya.R.B Department of Computer Engineering, APSIT


Semester: - VII Subject: Blockchain AY: 2024-25

Prof. Ramya.R.B Department of Computer Engineering, APSIT


Semester: - VII Subject: Blockchain AY: 2024-25

Multiple Inheritance

This Solidity program demonstrates multiple inheritance, where the C contract inherits from two parent
contracts, A and B. Contracts A and B each define an internal state variable (a and b, respectively)
and a function to set these variables (getA and getB). The C contract, which inherits from both A and
B, introduces a new function sum() that returns the sum of a and b.
The caller contract then creates an instance of C and interacts with it through two functions:
testInheritance() and seeInheritanceResult(). The testInheritance() function sets values for a and b by

Prof. Ramya.R.B Department of Computer Engineering, APSIT


Semester: - VII Subject: Blockchain AY: 2024-25

calling getA(15) and getB(15) on the instance of C, and then returns the sum of these values using
sum(). The seeInheritanceResult() function allows viewing the sum after it has been computed. This
example shows how multiple inheritance is used to combine functionality from multiple parent
contracts and how a separate contract (caller) can interact with the derived contract.

Prof. Ramya.R.B Department of Computer Engineering, APSIT


Semester: - VII Subject: Blockchain AY: 2024-25

Hierarchical Inheritance

This Solidity program illustrates hierarchical inheritance, where multiple derived contracts inherit from
a single base contract. The base contract, Vehicle, defines a state variable brand and a function
getBrand() to retrieve the brand name. Two derived contracts, Car and Bike, inherit from Vehicle.
The Car contract adds a new state variable numberOfDoors and a function getNumberOfDoors() to
manage the number of doors specific to a car. Similarly, the Bike contract introduces a state variable
hasCarrier and a function getHasCarrier() to indicate whether the bike has a carrier. Both derived
contracts use the constructor of Vehicle to initialize the brand variable while adding their own specific
attributes. This setup allows each derived contract to inherit common properties from Vehicle while
also extending the functionality to suit their specific needs.

Prof. Ramya.R.B Department of Computer Engineering, APSIT


Semester: - VII Subject: Blockchain AY: 2024-25

Prof. Ramya.R.B Department of Computer Engineering, APSIT


Semester: - VII Subject: Blockchain AY: 2024-25

Multilevel Inheritance

This Solidity program demonstrates multilevel inheritance, where a contract inherits from a derived
contract, forming a chain of inheritance. The base contract, A, defines a public string variable name
and a function getName() to retrieve it. The B contract inherits from A, meaning it has access to A's
state variables and functions. Additionally, B introduces its own string variable nameB and a function
getNameB() to manage it.
The C contract, which inherits from B, further extends the chain by introducing a new string variable
nameC and a function getNameC(). This multilevel inheritance structure allows C to inherit
properties and functions from both B and A, showcasing how contracts can build on one another in
Solidity. Each contract also initializes its specific name variable in its constructor, showing how each
layer can add its unique state while still inheriting from its parent contracts.

Prof. Ramya.R.B Department of Computer Engineering, APSIT


Semester: - VII Subject: Blockchain AY: 2024-25

Prof. Ramya.R.B Department of Computer Engineering, APSIT

You might also like