Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object. It is achieved through abstract classes and interfaces.
An abstract class is a class that cannot be instantiated directly. It is meant to be inherited by other classes. Abstract classes can contain abstract methods, which are methods without implementation, as well as concrete methods with implementation.
public abstract class Animal
{
public abstract void MakeSound();
public void Sleep()
{
Console.WriteLine("Sleeping...");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Barking...");
}
}
public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("Meowing...");
}
}
// Usage
Animal dog = new Dog();
dog.MakeSound(); // Output: Barking...
dog.Sleep(); // Output: Sleeping...
Animal cat = new Cat();
cat.MakeSound(); // Output: Meowing...
cat.Sleep(); // Output: Sleeping...
In this example, the Animal class is abstract and cannot be instantiated. It has an abstract method MakeSound which must be implemented by any non-abstract class that inherits from Animal. The Sleep method is a concrete method that provides a default implementation.
An interface is a completely abstract class that defines a set of methods and properties that a class must implement. Unlike abstract classes, interfaces do not contain any implementation.
public interface IAnimal
{
void MakeSound();
void Sleep();
}
public class Dog : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Barking...");
}
public void Sleep()
{
Console.WriteLine("Sleeping...");
}
}
public class Cat : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Meowing...");
}
public void Sleep()
{
Console.WriteLine("Sleeping...");
}
}
// Usage
IAnimal myDog = new Dog();
myDog.MakeSound(); // Output: Barking...
myDog.Sleep(); // Output: Sleeping...
IAnimal myCat = new Cat();
myCat.MakeSound(); // Output: Meowing...
myCat.Sleep(); // Output: Sleeping...
In this example, the IAnimal interface defines the contract for MakeSound and Sleep methods. Both Dog and Cat classes implement this interface and provide their own implementations for the methods.
Key Differences Between Abstract Classes and Interfaces
1. Implementation:
2. Multiple Inheritance:
3. Use Cases:
Conclusion
Abstraction is a powerful concept in C# that helps in creating a clear separation between what an object does and how it does it. By using abstract classes and interfaces, you can design flexible and maintainable code that can easily adapt to changes and extensions.
Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object. It is achieved through abstract classes and interfaces.
An abstract class is a class that cannot be instantiated directly. It is meant to be inherited by other classes. Abstract classes can contain abstract methods, which are methods without implementation, as well as concrete methods with implementation.
public abstract class Animal { public abstract void MakeSound(); public void Sleep() { Console.WriteLine("Sleeping..."); } } public class Dog : Animal { public override void MakeSound() { Console.WriteLine("Barking..."); } } public class Cat : Animal { public override void MakeSound() { Console.WriteLine("Meowing..."); } } // Usage Animal dog = new Dog(); dog.MakeSound(); // Output: Barking... dog.Sleep(); // Output: Sleeping... Animal cat = new Cat(); cat.MakeSound(); // Output: Meowing... cat.Sleep(); // Output: Sleeping...
In this example, the Animal class is abstract and cannot be instantiated. It has an abstract method MakeSound which must be implemented by any non-abstract class that inherits from Animal. The Sleep method is a concrete method that provides a default implementation.
An interface is a completely abstract class that defines a set of methods and properties that a class must implement. Unlike abstract classes, interfaces do not contain any implementation.
public interface IAnimal { void MakeSound(); void Sleep(); } public class Dog : IAnimal { public void MakeSound() { Console.WriteLine("Barking..."); } public void Sleep() { Console.WriteLine("Sleeping..."); } } public class Cat : IAnimal { public void MakeSound() { Console.WriteLine("Meowing..."); } public void Sleep() { Console.WriteLine("Sleeping..."); } } // Usage IAnimal myDog = new Dog(); myDog.MakeSound(); // Output: Barking... myDog.Sleep(); // Output: Sleeping... IAnimal myCat = new Cat(); myCat.MakeSound(); // Output: Meowing... myCat.Sleep(); // Output: Sleeping...
In this example, the IAnimal interface defines the contract for MakeSound and Sleep methods. Both Dog and Cat classes implement this interface and provide their own implementations for the methods.
Key Differences Between Abstract Classes and Interfaces
1. Implementation:
2. Multiple Inheritance:
3. Use Cases:
Conclusion
Abstraction is a powerful concept in C# that helps in creating a clear separation between what an object does and how it does it. By using abstract classes and interfaces, you can design flexible and maintainable code that can easily adapt to changes and extensions.
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved