In this article, we will see how to implement IDumpable interface through C#. The IDumpable interface is just a simple interface which has a Dump() method and public property. Every class that wishes to implement the IDumpable interface has to implement the Dump() method and can make use of the public property of interface to manage the execution of the code.
Let’s understand more in detail with a real-time example.
Let’s say you are driving a car and you would like to give a command in order to know whether a car needs to drive on Petrol/Diesel/CNG/Electricity. Here we will use the Dump() method to decide based on what command we would like to drive a car. Command will be given at run time through public interface property.
First, we will be defining Enums for different commands. DriveCommand enum will have 4 commands defined on which car will drive.
internal enum DriveCommand
{
PETROL,
DIESEL,
CNG,
ELECTRIC
}
Now let us define our IDumpable interface which will consist of the Dump() method and a public property called Command of type DriveCommand enum.
public interface IDumpable
{
DriveCommand Command
{
get;
set;
}
void Dump();
}
Now, let us create Car classes and inherit the IDumpable interface.
public class BMW : IDumpable
{
private DriveCommand _command;
private int _speed;
public DriveCommand Command
{
get { return _command; }
set { _command = value; }
}
public BMW(int Speed)
{
this._speed = Speed;
}
public void Dump()
{
Console.WriteLine("I AM DRIVING BMW");
Console.WriteLine("Command : " + _command);
if (_command == DriveCommand.PETROL)
{
Console.WriteLine("BMW IS NOW DRIVING ON " + _command +
" WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
}
else
{
Console.WriteLine("BMW IS NOT COMPATIBLE
TO DRIVE ON " + _command + "\n");
}
}
}
The BMW class consists of 2 private variables.
- _command : This is going to be a command given by the user to class BMW.
- _speed : Which is assigned via a parameterized constructor. This is going to be the speed at which Car will drive.
The Dump() method consists of our main logic to decide how the car will drive. As BMW is only driving on Petrol, it will check whether the given command is Petrol or not. If the command is Petrol, the method will prompt a message that the car is driving on Petrol at a given speed. But if the command is not petrol, then the method will prompt a message that the Car is not compatible to drive on the given command.
Let’s add a few more car classes with different driving modes.
public class TRUCK : IDumpable
{
private DriveCommand _command;
private int _speed;
public DriveCommand Command
{
get { return _command; }
set { _command = value; }
}
public TRUCK(int Speed)
{
this._speed = Speed;
}
public void Dump()
{
Console.WriteLine("I AM DRIVING TRUCK");
Console.WriteLine("Command : " + _command);
if (_command == DriveCommand.DIESEL)
{
Console.WriteLine("TRUCK IS NOW DRIVING ON " +
_command + " WITH SPEED OF " + _speed.ToString()
+ " KM/HR" + "\n");
}
else
{
Console.WriteLine("TRUCK IS NOT COMPATIBLE
TO DRIVE ON " + _command + "\n");
}
}
}
public class TUCSON : IDumpable
{
private DriveCommand _command;
private int _speed;
public DriveCommand Command
{
get { return _command; }
set { _command = value; }
}
public TUCSON(int Speed)
{
this._speed = Speed;
}
public void Dump()
{
Console.WriteLine("I AM DRIVING TUCSON");
Console.WriteLine("Command : " + _command);
if (_command == DriveCommand.PETROL
|| _command == DriveCommand.ELECTRIC)
{
Console.WriteLine("TUCSON IS NOW DRIVING ON " + _command
+ " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
}
else
{
Console.WriteLine("TUCSON IS NOT
COMPATIBLE TO DRIVE ON " + _command + "\n");
}
}
}
public class WAGONR : IDumpable
{
private DriveCommand _command;
private int _speed;
public DriveCommand Command
{
get { return _command; }
set { _command = value; }
}
public WAGONR(int Speed)
{
this._speed = Speed;
}
public void Dump()
{
Console.WriteLine("I AM DRIVING WAGONR");
Console.WriteLine("Command : " + _command);
if (_command == DriveCommand.PETROL || _command == DriveCommand.CNG)
{
Console.WriteLine("WAGONR IS NOW DRIVING ON " + _command
+ " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
}
else
{
Console.WriteLine("WAGONR IS NOT
COMPATIBLE TO DRIVE ON " + _command + "\n");
}
}
}
Let’s summarize all our classes and on what command they can run.
- Class BMW: Petrol
- Class Truck: Diesel
- Class Tucson: Petrol & Electric
- Class WagonR: Petrol & CNG
Now, let us call each Car’s Dump() method and see how it works. Before we call Dump() method, we will be passing speed to via parameterized constructor and Command to public property of interface.
static void Main(string[] args)
{
IDumpable Car = new BMW(90);
Car.Command = DriveCommand.PETROL;
Car.Dump();
Car.Command = DriveCommand.DIESEL;
Car.Dump();
Car = new TRUCK(100);
Car.Command = DriveCommand.DIESEL;
Car.Dump();
Car = new TUCSON(80);
Car.Command = DriveCommand.PETROL;
Car.Dump();
Car.Command = DriveCommand.ELECTRIC;
Car.Dump();
Car = new WAGONR(50);
Car.Command = DriveCommand.CNG;
Car.Dump();
Console.Read();
}
Example:
C#
using System;
namespace Demo {
internal class Program {
internal enum DriveCommand {
PETROL,
DIESEL,
CNG,
ELECTRIC
}
public interface IDumpable {
DriveCommand Command
{
get;
set;
}
void Dump();
}
public class BMW : IDumpable {
private DriveCommand _command;
private int _speed;
public DriveCommand Command
{
get { return _command; }
set { _command = value; }
}
public BMW(int Speed) { this._speed = Speed; }
public void Dump()
{
Console.WriteLine("I AM DRIVING BMW");
Console.WriteLine("Command : " + _command);
if (_command == DriveCommand.PETROL) {
Console.WriteLine(
"BMW IS NOW DRIVING ON " + _command
+ " WITH SPEED OF " + _speed.ToString()
+ " KM/HR"
+ "\n");
}
else {
Console.WriteLine("BMW IS NOT COMPATIBLE TO
DRIVE ON " + _command + "\n");
}
}
}
public class TRUCK : IDumpable {
private DriveCommand _command;
private int _speed;
public DriveCommand Command
{
get { return _command; }
set { _command = value; }
}
public TRUCK(int Speed) { this._speed = Speed; }
public void Dump()
{
Console.WriteLine("I AM DRIVING TRUCK");
Console.WriteLine("Command : " + _command);
if (_command == DriveCommand.DIESEL) {
Console.WriteLine(
"TRUCK IS NOW DRIVING ON " + _command
+ " WITH SPEED OF " + _speed.ToString()
+ " KM/HR"
+ "\n");
}
else {
Console.WriteLine(
"TRUCK IS NOT COMPATIBLE TO DRIVE ON "
+ _command + "\n");
}
}
}
public class TUCSON : IDumpable {
private DriveCommand _command;
private int _speed;
public DriveCommand Command
{
get { return _command; }
set { _command = value; }
}
public TUCSON(int Speed) { this._speed = Speed; }
public void Dump()
{
Console.WriteLine("I AM DRIVING TUCSON");
Console.WriteLine("Command : " + _command);
if (_command == DriveCommand.PETROL
|| _command == DriveCommand.ELECTRIC) {
Console.WriteLine(
"TUCSON IS NOW DRIVING ON " + _command
+ " WITH SPEED OF " + _speed.ToString()
+ " KM/HR"
+ "\n");
}
else {
Console.WriteLine("TUCSON IS NOT COMPATIBLE TO DRIVE
ON " + _command + "\n");
}
}
}
public class WAGONR : IDumpable {
private DriveCommand _command;
private int _speed;
public DriveCommand Command
{
get { return _command; }
set { _command = value; }
}
public WAGONR(int Speed) { this._speed = Speed; }
public void Dump()
{
Console.WriteLine("I AM DRIVING WAGONR");
Console.WriteLine("Command : " + _command);
if (_command == DriveCommand.PETROL
|| _command == DriveCommand.CNG) {
Console.WriteLine(
"WAGONR IS NOW DRIVING ON " + _command
+ " WITH SPEED OF " + _speed.ToString()
+ " KM/HR"
+ "\n");
}
else {
Console.WriteLine("WAGONR IS NOT COMPATIBLE TO DRIVE
ON " + _command + "\n");
}
}
}
static void Main(string[] args)
{
IDumpable Car = new BMW(90);
Car.Command = DriveCommand.PETROL;
Car.Dump();
Car.Command = DriveCommand.DIESEL;
Car.Dump();
Car = new TRUCK(100);
Car.Command = DriveCommand.DIESEL;
Car.Dump();
Car = new TUCSON(80);
Car.Command = DriveCommand.PETROL;
Car.Dump();
Car.Command = DriveCommand.ELECTRIC;
Car.Dump();
Car = new WAGONR(50);
Car.Command = DriveCommand.CNG;
Car.Dump();
Console.Read();
}
}
}
Output:
Similar Reads
C# Interface
An interface is defined using the interface keyword, similar to a class. An Interface is a blueprint that outlines a group of methods, properties, events, or indexers that a class or struct must implement. Unlike classes, it contains only the declaration of the members. The implementation of the int
3 min read
C# | Inheritance in interfaces
C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.Important Points: If a class implements an interface, then it is nece
3 min read
C# | Explicit Interface Implementation
An Interface is a collection of loosely bound items that have a common functionality or attributes. Interfaces contain method signatures, properties, events etc. Interfaces are used so that one class or struct can implement multiple behaviors. C# doesn't support the concept of Multiple Inheritance b
4 min read
SAP ABAP | Interfaces
ABAP(Advanced Business Application Programming) is an object-oriented programming language that supports many oops concepts like other programming languages. It supports all the four pillars of oops i.e. Inheritance, Polymorphism, Abstraction, and Encapsulation. The interface is one of the oops conc
4 min read
C# Program To Implement IDisposable Interface
IDisposable is an interface defined in the System namespace. It is used to release managed and unmanaged resources. Implementing IDisposable interface compels us to implement 2 methods and 1 boolean variable -Â Public Dispose() : This method will be called by the consumer of the object when resource
4 min read
Interface in Objective-C
Objective-C is an object-oriented programming language that was developed by Apple Inc. for its operating systems. One of the fundamental concepts of object-oriented programming is the ability to define interfaces, which specify the methods and properties that an object must have in order to be cons
7 min read
SAP Interfaces: A Complete Overview
An interface in SAP refers to a point of interaction between different software systems or components. These interfaces enable the exchange of data and communication between SAP and external applications, fostering integration and collaboration.SAP Interfaces: A Complete OverviewTable of ContentType
5 min read
C Identifiers
In C programming, identifiers are the names used to identify variables, functions, arrays, structures, or any other user-defined items. It is a name that uniquely identifies a program element and can be used to refer to it later in the program.Example:C// Creating a variable int val = 10; // Creatin
3 min read
C# Identifiers
In programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
C# | Type.GetInterface() Method
Type.GetInterface() Method is used to gets a specific interface implemented or inherited by the current Type. GetInterface(String) Method This method is used to search for the interface with the specified name. Syntax: public Type GetInterface (string name); Here, it takes the string containing the
4 min read