Friday, May 12, 2023

Opps part 1 : What is Inheritance?

 Inheritance is a concept in object-oriented programming (OOP) that allows a class (called the "child" or "derived" class) to inherit properties and methods from another class (called the "parent" or "base" class).

Inheritance promotes code reuse and allows developers to create new classes that are similar to existing classes, but with additional or modified functionality. The child class inherits all the public and protected properties and methods of the parent class, and can also add its own properties and methods.

For example, consider a class called "Animal" that has properties and methods for common attributes of all animals, such as "name", "age", and "eat()". A child class called "Cat" could inherit from the "Animal" class and add its own properties and methods, such as "purr()" or "scratch()". The "Cat" class can use the properties and methods of the "Animal" class, without having to rewrite them.

Inheritance is often used in software development to create a hierarchy of related classes that share common properties and behaviors, while also allowing for specialization and customization of each class.

Single Inheritance in C#

// Base class
class Animal
{
public void Eat()
{
Console.WriteLine("Animal is eating.");
}
}
// Derived class

class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Dog is barking.");
}
}
// Example usage

class Program
{
static void Main(string[] args)
{
// Create an instance of the Dog class
Dog myDog = new Dog();
// Call methods from the Dog class

myDog.Bark();
// Call inherited method from the Animal class

myDog.Eat();
}
}

MultiLevel Inheritance in C#


// Base class class Animal { public void Eat() { Console.WriteLine("Animal is eating."); } } // Derived class class Dog : Animal { public void Bark() { Console.WriteLine("Dog is barking."); } } // Derived class class Bulldog : Dog { public void Guard() { Console.WriteLine("Bulldog is guarding."); } } // Example usage class Program { static void Main(string[] args) { // Create an instance of the Bulldog class Bulldog myBulldog = new Bulldog(); // Call methods from the Bulldog class myBulldog.Bark(); myBulldog.Guard(); // Call inherited method from the Dog class myBulldog.Eat(); } }

Hierarchical Inheritance in C#


// Base class class Animal { public void Eat() { Console.WriteLine("Animal is eating."); } } // Derived class class Dog : Animal { public void Bark() { Console.WriteLine("Dog is barking."); } } // Derived class class Cat : Animal { public void Meow() { Console.WriteLine("Cat is meowing."); } } class Program { static void Main(string[] args) { Dog myDog = new Dog(); Cat myCat = new Cat(); myDog.Bark(); myCat.Meow(); myDog.Eat(); myCat.Eat(); } }

Multiple Inheritance in C#

C# does not support multiple Inheritance through classes; a class cannot inherit from more than one base class at the same time.

Note. C# does support multiple inheritances through interfaces.

interface IShape { void Draw(); } interface IColor { void SetColor(string color); } class Square : IShape, IColor { private string color; public void Draw() { Console.WriteLine("Drawing square."); } public void SetColor(string color) { this.color = color; } public void PrintColor() { Console.WriteLine("Color of square is: " + color); } } class Program { static void Main(string[] args) { Square square = new Square(); square.Draw(); square.SetColor("Blue"); square.PrintColor(); } }

 

No comments:

Post a Comment

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...