Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a must inherit class

Tags:

How to define a must inherit class? in C#

like image 566
thechmodmaster Avatar asked Jun 23 '12 21:06

thechmodmaster


People also ask

How do you inherit a class?

Inheritance is a feature or a process in which, new classes are created from the existing classes. The new class created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent class”. The derived class now is said to be inherited from the base class.

How do you inherit a class into another class in C#?

Inheritance (Derived and Base Class) In C#, it is possible to inherit fields and methods from one class to another. We group the "inheritance concept" into two categories: Derived Class (child) - the class that inherits from another class. Base Class (parent) - the class being inherited from.

Which classes Cannot be inherited?

An abstract class cannot be inherited by structures. It can contain constructors or destructors. It can implement functions with non-Abstract methods.

How do you inherit a class in Visual Basic?

Visual Basic introduces the following class-level statements and modifiers to support inheritance: Inherits statement — Specifies the base class. NotInheritable modifier — Prevents programmers from using the class as a base class. MustInherit modifier — Specifies that the class is intended for use as a base class only.


2 Answers

You mark the class as abstract (this is the C# analogue to the VB.NET Must Inherit).

This will ensure it can't be instantiated directly.

From the linked MSDN article:

The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

(emphasis mine)

like image 199
Oded Avatar answered Oct 26 '22 03:10

Oded


Use the abstract modifier.

public abstract class MyClass() {     ... } 
like image 45
Jeremy Wiggins Avatar answered Oct 26 '22 03:10

Jeremy Wiggins