Code design in C#: inheritance and abstract class

Before writing some app, a developer must have an idea how a code architecture will look like: what structures it will have, what behavior will methods or functions have, what will be names of structures, methods, functions etc. Moreover, the code should be readable and understandable for the author and the rest of a team. This is such a difficult task, especially when a client of the app needs more and more features. As a result, the code will be bigger and bigger. I would compare the situation with puzzles – the more pieces you have, the more difficult it is to find a proper place for them. Additionally, in coding, a developer has to give the pieces names 🙂

Programming languages offer some tools to help developers write more readable code. In C# two of them are inheritance and abstract class. This article describes the usage of inheritance and abstract class and how to write them in C# code.

A sample app

Let’s imagine a client who needs an app which has the following features:

  • cropping image in a rectangle shape,
  • cropping image in an ellipse shape and making rest of the image as transparent,
  • saving cropped image.

After reading the requirements, developer came up with an idea to create four methods with the following behaviors:

  • loading source image,
  • cropping image in an ellipse shape,
  • cropping image in a rectangle shape,
  • saving a target image.

How will developer put puzzles together?

First idea – just one class

Developer implemented one class ImageEditor with four methods.

Developer has accomplished the app. Nonetheless, a client wants additional feature: adding an icon with passed coordinates for both ellipse shape and rectangle shape images. However, for an ellipse, there should be a warning if a user will pass coordinates in a transparent place. Furthermore, there cannot be possible to add an icon.

Second idea – the one class and enum

Here is the code with new features.

Developer created one method to not duplicate code, which adds icon. In addition, developer implemented condition for ellipse shape cropping. After finished work, the client has suggested, he would like to have several other shapes of cropping, for example, star shape and triangle shape.

Developer realized that the condition in AddIcon method would have to be extended with other conditions for different shapes. Additionally, if a client wanted other features for several kinds of shapes, the class would be bigger and bigger.

The last idea – inheritance and abstract class

Developer started all over from scratch.

From now on, ImageEditor is an abstract class. Creating an object from an abstract class is not possible. An abstract class is a structure, which contains implementation for derived classes and definitions of methods that must be implemented by derived classes. What is a derived class? It is a class that inherits structures from a base class. This situation is called inheritance.

Instead of using a code of a base class, derived class has a possibility to override method of the base class, if the method has virtual keyword. However, it hasn’t to have own implementation and it can reproduce a code of the base class. In other words, the method will be the default for all derived classes.

Conclusion

To summarize, let’s have a look at benefits of inheritance and abstract class:

  • abstract class prevents from creating an object of a class,
  • abstract class forces derived classes into implementing abstract methods (not abstract classes mustn’t have abstract methods),
  • derived classes can have their own implementation of virtual method,
  • thanks to inheritance derived classes can use a code of a base class.

The code which literally crops images and adds an icon, you can find on my Github.

Share on

Leave a Reply

avatar
  Subscribe  
Notify of