C# Program to Inherit an Abstract Class and Interface in the Same Class
Last Updated :
15 Nov, 2021
Abstract Class is the way to achieve abstraction. It is a special class that never be instantiated directly. This class should contain at least one abstract method in it and mark by abstract keyword in the class definition. The main purpose of this class is to give a blueprint for derived classes and set some rules what the derived classes must implement when they inherit an abstract class. An abstract class can be used as a base class and all derived classes must implement the abstract definitions.
Syntax:
abstract class classname
{
// Method Declaration in abstract class
}
Here, the classname is the name of an abstract class. We can declare any number of methods inside it.
Interface is like a class, it can also have methods, properties, events, and indexers as its members. But interfaces can only have the declaration of the members. The implementation of the interface’s members will be given by the class that implements the interface implicitly or explicitly. Or we can say that it is the blueprint of the class.
Syntax:
interface interface_name
{
// Method Declaration in interface
}
Now given that one interface and one abstract class, now our task is to inherit both interface and abstract class in the same class.
Approach:
- Create an abstract class using abstract keyword and write a method definition for the abstract method.
- Create an interface using the interface keyword and write a method definition for Interface.
- Create a class with name GFG that will inherit both abstract class and interface like this:
class GFG : Abstract_Class, Interface
{
// Method definition for abstract method
// Method definition for interface
}
- Write the method definitions for both abstract class and interface in the GFG class.
- Inside the main, create two objects for both abstract class and interface.
- Call the methods using those objects and display the output.
Example:
C#
// C# program to illustrate how to inherit an
// abstract class and interface in the same class
using System;
// Abstract class
abstract class Abstract_Class
{
// Method declaration in abstract class
public abstract void abstract_method();
}
// Interface
interface Interface
{
// Method declaration in interface
void interface_method();
}
// Here, GFg class inherit abstract class and interface
class GFG : Abstract_Class, Interface
{
// Method definition for abstract method
public override void abstract_method()
{
Console.WriteLine("I am the method of abstract class");
}
// Method definition for interface
public void interface_method()
{
Console.WriteLine("I am the method of interface");
}
}
class Geeks{
// Driver code
public static void Main(String[] args)
{
// Creating objects
Abstract_Class ac = new GFG();
Interface inf = new GFG();
// Calling the methods of abstract class
// and interface
ac.abstract_method();
inf.interface_method();
}
}
Output:
I am the method of abstract class
I am the method of interface
Similar Reads
C# Program to Implement Multiple Interfaces in the Same Class Like a class, Interface can have methods, properties, events, and indexers as its members. But interface will contain only the declaration of the members. The implementation of interfaceâs members will be given by the class that implements the interface implicitly or explicitly. C# allows that a sin
3 min read
C# Program to Implement an Interface in a Structure Structure is a value type and a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types. We can create a structure by using struct keyword. A structure can also hold co
2 min read
Difference between Abstract Class and Interface in C# An abstract class is a way to achieve abstraction in C#. To declare an abstract class, we use the abstract keyword. An Abstract class is never intended to be instantiated directly. This class must contain at least one abstract method, which is marked by the keyword or modifier abstract in the class
4 min read
C# Program to Implement the Same Method in Multiple Classes C# is a general-purpose programming language it is used to create mobile apps, desktop apps, websites, and games. In C#, an object is a real-world entity. Or in other words, an object is a runtime entity that is created at runtime. It is an instance of a class. In this article, Implement the same me
3 min read
Are All Methods in a Java Interface are Abstract? In Java, the interface is called a blueprint of a class and it is used to achieve abstraction in java. By using interfaces only we can achieve multiple inheritances in java. Let's understand the concept of blueprint through an example like a blueprint of the building will consist of properties and b
8 min read