C# Program to Demonstrate Abstract Class with Multiple-level Inheritance
Last Updated :
01 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
Multiple-level Inheritance is a type of inheritance in which a derived class will inherit a base class and the derived class also behave like the base class to other class. For example, we have three classes named class1, class2, and class3. Here, class3 is derived from class2, and class2 is derived from class1.
Syntax:
class GFG : Abstract_Class
{
// Method definition for abstract method
}
MultiLevel Inheritance for Abstract Classes
Given an abstract class, our task is to implement the abstract class into the parent class and then implement the multilevel inheritance. So, let us understand with the help of an example.
class GFG : Abstract_class
{
// Method definition for abstract method
}
// First child class extends parent
class GFG2 : GFG
{
// Method definition
}
// Second child class extends first child class
class GFG3 : GFG2
{
// Method definition
}
Here, GFG is the parent class and GFG2 and GFG3 are the child class that extends the parent class.
Example:
C#
// C# program to illustrate abstract class
// with multiple-level Inheritance
using System;
// Abstract class
abstract class Abstract_class
{
// Method declaration for abstract class
public abstract void abstract_method();
}
// Parent class
class GFG : Abstract_class
{
// Method definition for abstract method
public override void abstract_method()
{
Console.WriteLine("Abstract method is called");
}
}
// First child class extends parent
class GFG2 : GFG
{
// Method definition
public void Mymethod1()
{
Console.WriteLine("Method from GFG2 class");
}
}
// Second child class extends first child class
class GFG3 : GFG2
{
// Method definition
public void Mymethod2()
{
Console.WriteLine("Method from GFG3 class");
}
}
class Geeks{
// Driver code
public static void Main(String[] args)
{
// Creating an object of GFG3 class
GFG3 obj = new GFG3();
// Call the methods using GFG3 class
obj.abstract_method();
obj.Mymethod1();
obj.Mymethod2();
}
}
Output:
Abstract method is called
Method from GFG2 class
Method from GFG3 class
Explanation: In this example, we created an Abstract class named "Abstract_class" and declared a method named "abstract_method" inside it. Then we created a parent class named "GFG" by overriding the method of an abstract class. After that, we created the first child class named "GFG2" that inherits the parent class and defined a method named "Mymethod1" inside it. Then created a second child class named "GFG3" that inherits the first child class and defined a method "Mymethod2" inside it. Finally, we created the main class that includes the main method, then created an object(named "obj") for the second child class and called all the methods that are declared and get the output on the screen.
Similar Reads
C# Program to Demonstrate Interface Implementation with Multi-level Inheritance Multilevel Inheritance is the process of extending parent classes to child classes in a level. In this type of inheritance, a child class will inherit a parent class, and as well as the child class also act as the parent class to other class which is created. For example, three classes called P, Q,
3 min read
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 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
C# Program to Inherit an Abstract Class and Interface in the Same Class 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 an
3 min read
How to Test an Abstract Class with JUnit? Testing abstract classes in Java can be challenging due to their incomplete nature. They cannot be instantiated directly. Abstract classes are often used as the blueprint for the other classes. They can include abstract methods, which lack implementations, as well as concrete methods that are fully
7 min read
Multiple Inheritance in Programming Multiple Inheritance in programming is like a family tree for your code. You have a bunch of classes, which are like different members of a family. When one class inherits from another, it's like a child inheriting traits from their parents. The child class gets all the abilities (methods) and prope
6 min read
Implement Interface using Abstract Class in Java Interface contains only abstract methods that can't be instantiated and it is declared by keyword interface. A class that is declared with the abstract keyword is known as an abstract class in Java. This is a class that usually contains at least one abstract method which can't be instantiated and It
3 min read
Multiple Inheritance in Python Inheritance is the mechanism to achieve the re-usability of code as one class(child class) can derive the properties of another class(parent class). It also provides transitivity ie. if class C inherits from P then all the sub-classes of C would also inherit from P. Multiple Inheritance When a class
5 min read
C# Program to Demonstrate the Static Constructor in Structure A Structure is a well-defined data structure that can hold elements of multiple data types. It is similar to a class because both are user-defined data types and both contain a bunch of different data types. You can also use pre-defined data types. However, sometimes the user might be in need to def
2 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