Welcome back! After learning to initialize objects with constructors, we’re ready to tackle another cornerstone of object-oriented programming: inheritance. This powerful feature allows us to create a new class based on an existing class, facilitating code reuse and organizing our programs more intuitively. Ready to explore how? Let’s dive in!
In this lesson, you will learn what inheritance is and how to use it in C#
. Specifically, you will understand how to:
Consider the following example, where we have a CelestialBody
class and a Planet
class that inherits from it:
C#1class CelestialBody 2{ 3 // Attribute 4 public string name; 5 6 // Constructor 7 public CelestialBody(string name) 8 { 9 this.name = name; 10 } 11} 12 13class Planet : CelestialBody 14{ 15 // Additional attribute specific to Planet class 16 public string orbitingStar; 17 18 // Constructor for both inherited and new attributes 19 public Planet(string name, string orbitingStar) : base(name) 20 { 21 this.orbitingStar = orbitingStar; 22 } 23 24 // Method specific to Planet class 25 public void Orbit() 26 { 27 Console.WriteLine(name + " is orbiting the star " + orbitingStar); 28 } 29}
By the end of this lesson, you will be able to create hierarchical class structures, fostering better code organization and reuse.
When creating a derived class, you often need to initialize both the inherited attributes and any new attributes specific to the derived class. This is done through the derived class's constructor.
In the Planet
class, we see an example of this:
C#1public Planet(string name, string orbitingStar) : base(name) 2{ 3 this.orbitingStar = orbitingStar; 4}
Here's a breakdown of how this works:
public Planet(string name, string orbitingStar)
: This defines the constructor for the Planet
class, accepting parameters for both the inherited and new attributes.: base(name)
: The base
keyword is used to call the constructor of the base class (CelestialBody
). This initializes the name
attribute inherited from CelestialBody
.this.orbitingStar = orbitingStar;
: We ensure that the orbitingStar
attribute of the child class is correctly initialized.By correctly using constructors, you ensure that all attributes, both inherited and new, are properly initialized when an object of the derived class is created.
For inheritance to work effectively, the attributes and methods of your base class should be either public
or protected
. Public
members are accessible from any other code, while protected
members are accessible within their own class and by derived class instances.
Using the right access modifiers ensures that your derived classes can properly inherit and utilize the attributes and methods from the base class.
Understanding inheritance is crucial for several reasons:
For instance, by inheriting from CelestialBody
to create a Planet
, you can add specific attributes and methods while retaining common features. This way, you can expand your codebase without redundancy.
Excited? Let’s proceed to the practice section and apply these concepts to solidify our understanding.