Understanding Contracts and Objects in Solidity
Solidity, a popular programming language for Ethereum smart contracts, is an object-oriented language similar to Java and C++.
This blog looks at the basic ideas of contracts and objects in Solidity, helping both new and experienced developers understand its key parts.
What are Contracts and Objects in Solidity?
In Solidity, contracts and objects work like classes and objects in other object-oriented programming languages. Here's a quick overview:
Contracts: These are like classes in Java or C++. They serve as blueprints for creating instances that include properties and behaviors.
Objects: These are instances of contracts, created using the
new
keyword.
If you know about classes and objects, understanding contracts in Solidity will be easy. For beginners, it might seem complicated at first, but this blog will make the ideas simple.
Contracts as Classes
A class in regular programming describes an object’s properties and behavior. In the same way, in Solidity, a contract outlines the properties and functions that define an object.
For example:
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract Book {
uint length;
uint breadth;
uint height;
function setDimension(uint _length, uint _breadth, uint _height) public {
length = _length;
breadth = _breadth;
height = _height;
}
function getDimension() public view returns (uint, uint, uint) {
return (length, breadth, height);
}
}
In the example above:
The
Book
contract defines three properties (length
,breadth
,height
) and two functions (setDimension
andgetDimension
).It serves as a blueprint to create objects with specific dimensions.
Creating Objects in Solidity
To create an object from a contract, we use the new
keyword. This sets up a contract and gives us an object that refers to the contract in memory. For example:
Book obj = new Book();
Here:
obj
is an object of typeBook
.The
new
keyword creates an instance of theBook
contract, enabling access to its properties and methods.
Example: Using Contracts and Objects Together
Let’s create another contract, D
, which interacts with the Book
contract by creating an object of Book
and accessing its functions.
contract D {
string title;
string author;
// Create an object of the Book contract
Book obj = new Book();
function getObject() public view returns(Book) {
return obj;
}
function writeDimension(uint _length, uint _breadth, uint _height) public {
obj.setDimension(_length, _breadth, _height);
}
function readDimension() public view returns (uint, uint, uint) {
return obj.getDimension();
}
function setBookTitleAuthor(string memory _title, string memory _author) public {
title = _title;
author = _author;
}
function getBookTitle() public view returns (string memory) {
return title;
}
function getBookAuthor() public view returns (string memory) {
return author;
}
}
In this example:
The
D
contract creates an objectobj
of typeBook
.The
D
contract defines two additional properties (title
,author
) and three functions.The
setBookTitleAuthor
function sets thetitle
andauthor
properties.The
getBookTitle
function retrieves the title of the book.The
getBookAuthor
function retrieves the author of the book.
Deployment and Testing
Deploy both
Book
andD
contracts using Remix IDE.Open Remix IDE and deploy both
Book
andD
contracts.Use the
D
contract to:Set
writeDimension
like10, 20, 30
.Retrieve the
readDimension
function.Set the title and author of the book using the
setBookTitleAuthor
function.Retrieve the title of the book using the
getBookTitle
function.Retrieve the author of the book using the
getBookAuthor
function.
Why Use Contracts and Objects in Solidity?
Object-oriented principles are popular in programming because they help with modularity and reusability. By treating contracts like classes and creating objects:
Scalability: You can break down complex projects into smaller, reusable parts.
Modularity: Objects keep data and behavior together, making code easier to maintain.
Industry Standards: Many large projects use this approach to organize their Solidity applications well.
Key Takeaways
Contracts in Solidity are like classes in other programming languages.
Objects let you use a contract’s functions and properties.
Learning these ideas is crucial for creating strong and scalable Solidity applications.
With regular practice and experience in real projects, you'll understand the strength of contracts and objects in Solidity. Whether you're building a decentralized application (dApp) or exploring blockchain development, these principles are the basics of Solidity programming.