What Is a Constructor, Inheritance, and a Class in Sofware Development

Software Engineer | Developer Advocate | Technical Writer | Content Creator
Search for a command to run...

Software Engineer | Developer Advocate | Technical Writer | Content Creator
Nice read! I love the fact that you included examples in multiple programming languages! 👏
Thank you for reading 👏
Thank you for reading
Thanks for providing examples in both languages. That really helps!
Great article.
Thank you for reading
In this series, I'll be sharing incredible content on How- to's, Building and Deploying Applications using Modern Web Technologies.
Error handling in Node js is one of the most important things to know as a developer using NodeJS to build amazing software. In this article, I will be showing you how to handle errors in NodeJS and also creating a global error handler to handle unfo...
Step-by-Step Solidity Guide for Beginners: Building Your First Token

Prioritizing Depth Instead of Going with Trends

Cross-chain messaging allows seamless communication and interaction between different blockchain ecosystems, from Ethereum Virtual Machine (EVM)-based chains to Cosmos-based chains. Axelar has long been the best way to connect EVM and Cosmos chains v...

You have millions of unique images in your gallery, each representing a piece of private or personal data stored on your phone. After an incident, you lost your device, but it was returned to you after many months, with claims that the images hadn't ...

Marking a Year of Contributions to Axelar: Pioneering the Future of Interoperability

uh yeah! I trust you are doing great? Today, I will be explaining everything you need to know that would help foster your understanding of classes, inheritance, and constructors in software development.
Understanding the concept behind these terms might seems to be difficult when you are just learning how to code or you probably trying to level up and improve more on your existing skillset.
A constructor is a special method in C# and other programming languages that is automatically invoked at the time of object creation. Generally, it is used to configure the new object data members. The constructor in C# has the same name as the structure or class.
In simpler terms, think of it as something that runs first when an object of a class is created. I will be explaining what classes are and how a constructor can be created in a class shortly.
The term constructor can be used in every other programming languages but I will show you examples in two different programming languages(C# and JavaScript)
Constructors with parameters are constructors that have parameters and can be used to provide distinct objects with various values.
using System;
public class Student
{
// Variable declaration
public int id;
public String name;
public float income;
// a constructor with parameters
public Student(int studentId, String studentName, float studentIncome)
{
id = studentId;
name = studentName;
income = studentIncome;
}
// Display student ID, Name and Income
public void display()
{
Console.WriteLine(id + " ," + name + " ," + income);
}
}
// Test the class to validate the work of a constructor
class StudentTest{
public static void Main(string[] args)
{
// Create the instance of the class
Student studentA = new Student(001, "Ade", 590000);
Student studentB = new Student(012, "Alesh", 909000);
// Call the display function in the class created earlier
studentA.display();
studentA.display();
}
}
// OUTPUT:
// 001 , Ade , 590000
// 012 , Alesh , 909000
class Student {
// a constructor with parameter
constructor(studentId, studentName, studentIncome) {
this.studentId = studentId;
this.studentName = studentName;
this.studentIncome = studentIncome;
}
display() {
// Log result
console.log(
`Hello, my name is ${this.studentName} with id ${this.studentId} and income: ${this.studentIncome}`
);
}
}
const selectStudentA = new Student(1, 'Ade', 490000);
const selectStudentB = new Student(2, 'Alesh', 590000);
selectStudentA.display();
selectStudentB.display();
// OUTPUT:
// Hello, my name is Ade with id 1 and income: 490000
// Hello, my name is Alesh with id 2 and income: 590000
As a default constructor, a constructor that has no argument is defined. It is invoked at the moment of object formation.
using System;
public class Student
{
// a constructor
public Student()
{
Console.WriteLine("Constructor invoked successfully");
}
}
// Test the class to validate the work of a constructor
public static void Main(string[] args)
{
// Create the instance of the class
Student studentA = new Student();
Student studentB = new Student();
}
// OUTPUT:
// Constructor invoked successfully
// Constructor invoked successfully
class Student {
// a constructor with parameter
Student() {
console.log('Constructor invoked successfully');
}
}
const selectStudentA = new Student();
const selectStudentB = new Student();
// OUTPUT:
// Constructor invoked successfully
// Constructor invoked successfully
Inheritance is a mechanism in which all the properties and behaviors of its parent object are inherited automatically by one object.
In this way, it is possible to reuse, expand or change the attributes and behaviors which are described in another class.
using System;
// create a class
// parent class
public class Student
{
public float income = 90000;
}
// create another class to inherit from the student class
// Derived class
public class Best: Student
{
public float bonus = 10000;
}
class BestStudent{
public static void Main(string[] args)
{
Best student = new Best();
Console.WriteLine("Income: " + student.income);
Console.WriteLine("Bonus: " + student.bonus);
}
}
// OUTPUT:
// Income: 90000
// Bonus: 10000
// create a class
// parent class
class Student {
constructor() {
this.income = 90000;
}
}
// create another class to inherit from the student class
// Derived class
class Best extends Student {
constructor() {
super();
this.bonus = 10000;
}
}
const bestStudent = new Best();
console.log(`Income: ${bestStudent.income}`);
console.log(`Bonus: ${bestStudent.bonus}`);
// OUTPUT:
// Income: 90000
// Bonus: 10000
I'm pretty sure by now you can guess what a class can be referred to.
A class is like a particular object blueprint. Every object has some color, form, and functionality in the real world - the Range Rover luxury car, for example. The Range Rover is a luxury car-type object. The luxury car is a class that displays certain features, such as speed, color, form, interior, etc.
Example of a class
// create a class
class Student
{
// a class can contain a constructor
// a class can also contain a method
public float income = 90000;
}
I hope you now completely understand inheritance, constructor, and I hope the C# and Javascript examples ease the learning process.
Don't forget to share and give a thumbs up 😊
I'd love to connect with you at Twitter | LinkedIn | GitHub
See you in my next blog article. Take care!!!