C# in Simple Terms - Interfaces and Abstract Classes

C# in Simple Terms - Interfaces and Abstract Classes

First, we'll discuss a way to allowing class to implement a common set of property and method definitions (interfaces), and then we'll discuss a manner by which a class can be defined but only partially implemented so that its derived classes can use their own implementation (abstract classes). We will also point out some important difference between interfaces and abstract classes along the way.

First, we'll discuss a way to allowing class to implement a common set of property and method definitions (interfaces ), and then we'll discuss a manner by which a class can be defined but only partially implemented so that its derived classes can use their own implementation (abstract classes ). We will also point out some important difference between interfaces and abstract classes along the way.

´╗┐Interfaces

Interfaces are special objects in C## that defines a set of related functionalities which may include methods, properties, and other members. Think of interfaces as a contract, one where classes that implement an interface agree to provide implementations for all objects defined by that interface.

Interfaces cannot contain any implementations, and their names are generally prefixed with "I" to distinguish them from other C## objects. We create interfaces using the interface keyword:

public interface IAreaCalculator
{
    double GetArea();
}

Classes and structs can then implement an interface and define the behavior of the interface's methods:

public class Circle : IAreaCalculator
{
    public double Radius { get; set; }

    public double GetArea()
    {
        return Math.PI * (Radius * Radius);
    }
}

public class Rectangle : IAreaCalculator
{
    public double Height { get; set; }
    public double Width { get; set; }

    public double GetArea()
    {
        return Height * Width;
    }
}

public class Triangle : IAreaCalculator
{
    public double Height { get; set; }
    public double Width { get; set; }

    public double GetArea()
    {
        return Height * Width * 0.5;
    }
}

c# in simple terms c++

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

C# in Simple Terms - Operators

Let's continue our C# in Simple Terms series with one of the basic parts of any line of code: the operators.

C# in Simple Terms - Structs and Enums

Now that we understand a little more about classes and previously learned the difference between value types and reference types, it's time to explore some more specialized C# types. In today's edition of C# in Simple Terms, let's explore two useful value types: structs and enums.

C# in Simple Terms - Namespaces

All code needs some kind of organization, and the most basic way to provide that organization in C# programs is through the use of namespaces.

Introducing C# in Simple Terms!

In the past, we have used mega-series to tackle big subjects such as design patterns, anti-patterns, and sorting algorithms. In this series, we're going back to basics to discover, learn, and teach the programming language we all know and love: C#!

C# in Simple Terms - Inheritance and Polymorphism

Now that we've discussed most of the basics we need for a C# program, let's talk about two concepts that are central to how C# (and indeed, all object-oriented programming languages) work: inheritance and polymorphism.