Giter VIP home page Giter VIP logo

csharp1_polymorphism's Introduction

Csharp1_polymorphism

In C#, polymorphism refers to the ability of an object to take on many forms or types. It allows objects of different classes to be treated as objects of a common base class during runtime.

Polymorphism is achieved through two main mechanisms: inheritance and method overriding. Let's explore each of these in more detail:

Inheritance: In C#, you can define a class that inherits from another class, also known as the base class or parent class. The derived class can inherit the properties, methods, and behaviors of the base class. Polymorphism comes into play when you have a reference variable of the base class type that is actually referring to an object of the derived class. Here's an example:

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 Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat meows.");
    }
}

// Usage:
Animal animal1 = new Dog();
Animal animal2 = new Cat();

animal1.MakeSound(); // Output: "The dog barks."
animal2.MakeSound(); // Output: "The cat meows."

In the example above, both the Dog and Cat classes inherit from the Animal base class. The MakeSound method is overridden in each derived class to provide a specific implementation. When we create objects of the derived classes and assign them to variables of the base class type, we can call the MakeSound method on these variables, and the appropriate implementation based on the actual object type will be executed.

Method Overriding: In the previous example, the MakeSound method in the base class is marked as virtual, and the corresponding methods in the derived classes are marked as override. This combination enables method overriding, which allows a derived class to provide its own implementation of a method that is already defined in the base class. By using method overriding, you can invoke a method on a base class reference variable, but the overridden method in the derived class will be called at runtime. This is another form of polymorphism.

It's important to note that polymorphism relies on the base class reference variable to access the overridden method in the derived class. If the method is not overridden, the base class's implementation will be executed.

Polymorphism is a fundamental concept in object-oriented programming that promotes code reusability, flexibility, and extensibility. It allows you to write more generic and flexible code that can work with objects of different types, as long as they share a common base class or interface.

csharp1_polymorphism's People

Contributors

luiscoco avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.