Implement the concept of Overloading, Overriding, constructor and Inheritance in C#.Net
1. Overloading
Overloading in C# allows multiple methods to have the same name, but different parameters. The compiler will differentiate between the methods based on the number, type, and order of the parameters. Here’s an example:
using System;
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
class Program
{
static void Main(string[] args)
{
Calculator calculator = new Calculator();
int sum1 = calculator.Add(2, 3);
Console.WriteLine("2 + 3 = " + sum1);
double sum2 = calculator.Add(2.5, 3.5);
Console.WriteLine("2.5 + 3.5 = " + sum2);
int sum3 = calculator.Add(2, 3, 4);
Console.WriteLine("2 + 3 + 4 = " + sum3);
}
}
Output

2. Overriding
Overriding in C# allows a subclass to provide a specific implementation of a method that is already defined in its superclass. Here’s an example
using System;
class Animal {
public virtual void MakeSound() {
Console.WriteLine("The animal makes a sound");
}
}
class Dog : Animal {
public override void MakeSound() {
Console.WriteLine("The dog barks");
}
}
class Program {
static void Main(string[] args) {
Animal myAnimal = new Animal();
Dog myDog = new Dog();
myAnimal.MakeSound();
myDog.MakeSound();
}
}
Output

3. Constructors
Constructors in C# are special methods that are called when an instance of a class is created. They are used to initialize the state of an object. Here’s an example:
using System;
class Person
{
// Fields
private string name;
private int age;
// Constructors
public Person()
{
name = "John Doe";
age = 0;
}
public Person(string name)
{
this.name = name;
age = 0;
}
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
// Methods
public void Display()
{
Console.WriteLine("Name: {0}, Age: {1}", name, age);
}
}
class Program
{
static void Main(string[] args)
{
Person person1 = new Person();
Person person2 = new Person("Jane Smith");
Person person3 = new Person("Bob Johnson", 30);
person1.Display(); // Output: Name: John Doe, Age: 0
person2.Display(); // Output: Name: Jane Smith, Age: 0
person3.Display(); // Output: Name: Bob Johnson, Age: 30
}
}
Output

4. Inheritance
Inheritance in C# allows a class to inherit the properties and methods of another class. The class that is being inherited from is called the superclass or base class, while the class that is inheriting is called the subclass or derived class. Here’s an example
using System;
// Base class
class Animal
{
public void Eat()
{
Console.WriteLine("The animal is eating.");
}
}
// Derived class
class Cat : Animal
{
public void Meow()
{
Console.WriteLine("The cat is meowing.");
}
}
// Main program
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat();
cat.Eat(); // inherited method
cat.Meow(); // derived method
}
}
Output

SELECT AVG(emp_sal)
FROM employee;
SELECT COUNT(*)
FROM employee;