C# Program to Demonstrate the Inheritance of Abstract Classes
Last Updated :
23 Nov, 2021
Abstraction is the process to hide the internal details and show only the functionality. The abstract keyword is used before the class or method to declare the class or method as abstract. And inheritance is the object-oriented programming methodology by which one class is allowed to inherit the features(fields and methods) of another class. In C#, we can also inherit the abstract class using the : operator.
abstract class Abstract_class
{
// Method declaration for abstract class
public abstract void abstract_method();
}
class class_name : Abstract_class
{
// Method definition for abstract method
}
Approach:
- Create an abstract class and declare a method inside it using abstract keyword.
- Create a parent class by overriding the method of an abstract class.
- Create first child class that inherits the parent class and define a method inside it.
- Create an object that is “Geeks1 obj = new Geeks1()” for the first child class in the main method.
- Call all the methods that are declared using obj object.
Example:
C#
using System;
abstract class Abstract_class
{
public abstract void abstract_method();
}
class Geeks : Abstract_class
{
public override void abstract_method()
{
Console.WriteLine( "Abstract method is called" );
}
}
class Geeks1 : Geeks
{
public void method1()
{
Console.WriteLine( "method-1 is called" );
}
}
class GFG{
public static void Main(String[] args)
{
Geeks1 obj = new Geeks1();
obj.abstract_method();
obj.method1();
}
}
|
Output:
Abstract method is called
method-1 is called
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...