Inheritance in C# Language

Submitted by: 
Language: 

Objective

Inheritance in programming language makes sure that a class can reuse some features of his parent class (without rewriting all methods/functions that has implemented somewhere). In this tutorial, we will show how to implement class inheritance in an easy way.

Let's go

Just as children inherit characteristics and behaviors (also known as functions) from their parents, classes can inherit characteristics and behavior as well. Inheritance, also called derivation, in C# language enables a new class to be created 9called a child or derived class) that inherits the characteristics and behaviors from its parents (called base classes).
Inheritance also enables derived class to make several changes from their base class. The derived class can do the following:

  • Add new private data
  • Add new behavior
  • Redefine existing behavior

Let see bellow code to get more understanding of inheritance:

  1. public class Vehicle { };
  2.  
  3. public class FourWheeledVehicle : Vehicle { };
  4.  
  5. public class TwoWheeledVehicle : Vehicle { };
  6.  
  7. public class Car : FourWheeledVehicle { };
  8.  
  9. public class Truck : FourWheeledVehicle { };
  10.  
  11. public class MotoCycle : TwoWheeledVehicle { };

Here we declare a parent (base) class named Vehicle. Next, we declare two childs class that inherit the Vehicle named FourWheeledVehicle and TwoWheeledVehicle. We then declare three classes as child of two classes above (so the three classes are grandchild of Vehicle class).
In this case, FourWheeledVehicle and TwoWheeledVehicle will have all features of Vehicle class, an can implement additional private data/ behaviors. This is true for three classes, for example, Car class will inherit all features of FourWheeledVehicle (and of course all features of Vehicle class).

Adding new private data
Let see a snippet code bellow about adding new private data to child (retrieved) class:

  1. public class Vehicle {
  2.  
  3. protected int wheel_radius;
  4.  
  5. protected String color;
  6.  
  7. protected String manufacture;
  8.  
  9. };
  10.  
  11. public class FourWheeledVehicle : Vehicle {
  12.  
  13. protected Boolean has_conditioner;
  14.  
  15. };

Note that the class FourWheeledVehicle has all three private data includes “wheel_radius”, “color” and “manufacture” (inherits from Vehicle), and has additional private data “has_conditioner” that the parent (base) class Vehicle doesn’t has.

Adding new behavior
Child class can easily add new behavior, like bellow:

  1. public class Vehicle {
  2.  
  3. protected int wheel_radius;
  4.  
  5. protected String color;
  6.  
  7. protected String manufacture;
  8.  
  9. //this is base behavior
  10. public Vehicle()
  11. {
  12. wheel_radius = 1;
  13. color = "red";
  14. manufacture = "toyota";
  15. }
  16. };
  17.  
  18. public class FourWheeledVehicle : Vehicle {
  19.  
  20. private Boolean has_conditioner;
  21.  
  22. //new behavior is added
  23. public FourWheeledVehicle(int wheel_radius, String color, String manufacture)
  24. {
  25. this.wheel_radius = wheel_radius;
  26. this.color=color;
  27. this.manufacture = manufacture;
  28. }
  29. };

In above example, the class FourWheeledVehicle not only has behavior/function initializer that Vehicle has declared (Vehicle() without parameter), but also has additional behavior with three parameters (initializer name FourWheeledVehicle with three input parameters).


Redefine existing behavior

We can change the existed behavior/function of base class to make a polymorphis program. Let see code as follows:

  1. public class Vehicle {
  2.  
  3. protected int wheel_radius;
  4.  
  5. protected String color;
  6.  
  7. protected String manufacture;
  8.  
  9. protected int speed;
  10.  
  11. //this is base behavior
  12. public Vehicle()
  13. {
  14. wheel_radius = 1;
  15. color = "red";
  16. manufacture = "toyota";
  17. }
  18.  
  19. public void increaseSpeed()
  20. {
  21. speed = speed + 1;
  22. }
  23. };
  24.  
  25. public class FourWheeledVehicle : Vehicle {
  26.  
  27. private Boolean has_conditioner;
  28.  
  29. //new behavior is added
  30. public FourWheeledVehicle(int wheel_radius, String color, String manufacture)
  31. {
  32. this.wheel_radius = wheel_radius;
  33. this.color=color;
  34. this.manufacture = manufacture;
  35. }
  36.  
  37. public void increaseSpeed()
  38. {
  39. speed = speed + 2;
  40. }
  41. };

Asumming that the base class Vehicle has a speed data that contains the speed value of the Vehicle. In the base class Vehicle, the function increaseSpeed only increases 1 to speed of the Vehicle, but in the child class FourWheeledVehicle, we redeclare this function and increase 2 to speed of Vehicle. If we don’t declare the increaseSpeed in FourWheeledVehicle class, so this class will has increaseSpeed function that increases 1 to speed.

Summary

This tutorial investigated about Inheritance, an important definition in C# language. With inheritance, you can reuse and simplify your code and structure of program, make it clear and easy for writing code. For complexity application, inheritance makes sure that you application can structure in the right way and easy to maintain.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.