Deep C# - Inheritance
Written by MIke James   
Thursday, 27 August 2020
Article Index
Deep C# - Inheritance
Changing Types
Hiding Methods
Summary

To be clear:

  • A class that inherits from an interface doesn’t inherit any methods, only method declarations.

What good does this do?

The role of interfaces in object-oriented programming, indeed whether they have a role, is something that can be discussed at length without coming to any strong conclusion.

An interface is very similar to the idea of a pure abstract class. If you declare a class as abstract then any methods that are also declared as abstract don’t have definitions and have to be overridden by any class that inherits the abstract class – unless it too is declared to be abstract. An abstract class can’t be used to create objects and it only becomes “non-abstract” when all of the abstract methods have been overridden.

You can see that abstract classes allow the definition of an inheritance chain that adds more implementation code at each step. An interface on the other hand demands that every method it declares is implemented the very first time it is inherited – in this sense it’s a much cruder instrument.

The intended use of an interface is to ensure that all classes that inherit from it have the methods that it declares. If a class doesn’t implement all of the interface methods exactly then the class doesn’t compile. You can think of it as a template for a class and its use is to ensure that classes present a uniform and enforced set of methods that can be used with confidence by other objects. Used in this way it is a central part of most modern programming methodologies.

One big difference between class and Interface inheritance is that Interfaces support multiple inheritance. You can force a class to implement two Interface specifications using

public class MyClass:IMyInterface1,
IMyInterface2

This looks like multiple inheritance but as nothing but the method definitions are inherited it isn’t nearly so dangerous or, of course, as useful!

Some other facts worth knowing about interfaces are that a struct can inherit an interface and an interface can inherit from other interfaces. Of course an interface that inherits from another set of interfaces doesn’t have to implement anything and it simply serves to extend or add to the base interfaces.

 

Deep C#

 front

 Chapter List

  1. Value And Reference
  2. Dynamic C#
  3. Passing Parameters
  4. Inheritance ***NEW
  5. Casting – the escape from strong typing
  6. Controlling Inheritance
  7. Delegates
  8. Multicast delegates and events
  9. Anonymous Methods, Lambdas And Closures
  10. Take Exception To Everything
  11. What's The Matter With Pointers?
  12. Generics
  13. Structs
  14. The LINQ Principle
  15. Regular Expressions in depth
  16. Bit Manipulation
  17. Async
  18. The Parallel For
Multicast delegates and events
Tuesday, 25 May 2010
Article Index
Multicast delegates and events
Events
Generic Events

Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you know you could add and subtract delegates?

square

 



 

Comments




or email your comment to: comments@i-programmer.info

 

 



Last Updated ( Thursday, 27 August 2020 )