C# Program to Demonstrate the Inheritance of Abstract Classes
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 “Beginner1 obj = new Beginner1()” for the first child class in the main method.
- Call all the methods that are declared using obj object.
Example:
C#
// C# program to inherit abstract class. using System; // Abstract class abstract class Abstract_class { // Method Declaration for abstract class public abstract void abstract_method(); } // Parent class class Beginner : Abstract_class { // Method definition for abstract method public override void abstract_method() { Console.WriteLine( "Abstract method is called" ); } } // First child class extends parent class Beginner1 : Beginner { // Method definition public void method1() { Console.WriteLine( "method-1 is called" ); } } class GFG{ // Driver code public static void Main(String[] args) { // Create an object Beginner1 obj = new Beginner1(); // Call the methods obj.abstract_method(); obj.method1(); } } |
Output:
Abstract method is called method-1 is called
Contact Us