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

Subscribe to my newsletter and never miss my upcoming articles

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.

What is a Constructor?

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.

In C# we have two types of constructors, which are

  • Constructor with parameters
  • Default constructor

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)

Constructor with parameters

Constructors with parameters are constructors that have parameters and can be used to provide distinct objects with various values.

Example of a constructor with parameters with CSharp
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
Example of a constructor with parameters with JavaScript
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

Default constructor

As a default constructor, a constructor that has no argument is defined. It is invoked at the moment of object formation.

Example of Default constructor with CSharp
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
Example of Default constructor with JavaScript
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

What is Inheritance?

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.

Inheritance Concepts

  • Base Class (parent) - from which the class is inherited
  • Class derived (child) - the class inherited from another class
Example of Inheritance with CSharp
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
Example of Inheritance with JavaScript
// 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

What is a Class?

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;  
   }

Conclusion

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 😊

You can also reach out to me on Twitter

Catalin's Tech's photo

Nice read! I love the fact that you included examples in multiple programming languages! 👏

Olubisi Idris Ayinde's photo

Thank you for reading 👏

ƤƗỮŞ ΜỮĦỮΜỮŽΔ's photo

Great read.

Olubisi Idris Ayinde's photo

Thank you for reading

Bell Omuboye's photo

Thanks for providing examples in both languages. That really helps!

Great article.

Olubisi Idris Ayinde's photo

Thank you for reading