Learning to Code — Part 9a: Inheritance & Polymorphism

Scott Rosenbloom
Aug 3, 2018 · 10 min read

INHERITANCE

Next up, we go into the Inheritance & Polymorphism section of the SoloLearn app. We definitely talked about polymorphism early on, so I’m hoping to understand that pretty well when we get to it. First, though, we’ll talk about inheritance. The is the ability to define one class based on another class. Apparently, this…

class Animal {
public int Legs { get; set; }
public int Age { get; set; }
}
class Dog : Animal {
public Dog() {
Legs = 4;
}
}
public void Bark() {
Console.Write(“Woof”);
}
}
static void Main(string[] args) {
Dog d = new Dog();
Console.WriteLine(d.Legs);
d.Bark();
}
  • Then, we jump to the Dog class, where the constructor method called Dog is immediately (and automatically) run. Within it, a seemingly undeclared variable called Legs is mentioned. But, since the Dog class gets all of the features of the Animal class, a publicly accessible, integer-based variable called Legs is declared.
  • It then gets the value assigned to it from where it was called. That was in the Dog class where, in the constructor method called Dog, the variable Legs was assigned (or set) the value of 4.
  • Jumping back to the Main method, the next line says to print to the screen, the value of the variable called Legs within the instantiation of the Dog class called d. As this is 4, 4 is printed to the screen.
  • Finally, the method called Bark is called within the instantiation of the Dog class called d, which says to print the word “Woof” to the screen.

PROTECTED MEMBERS

So far, we’ve only talked about public (members accessible from anywhere outside of the class) and private (members accessible only from within their classes) access modifiers. Protected members are similar to private members, accept that they can also be access from derived classes.

class Person {
protected int Age { get; set; }
protected string Name { get; set; }
}
class Student : Person {
public Student(string nm) {
Name = nm;
}
public void Speak() {
Console.Write(“Name: “ + Name);
}
}
class Program {
static void Main(string[] args) {
Student s = new Student(“David”);
s.Speak();
}
}
static void Main(string[] args) {
Student s = new Student(“David”);
s.Name();
}
sealed class Animal {
//some code
}
class Dog : Animal {}

DERIVED CLASS CONSTRUCTOR & DESTRUCTOR

Next, the app talks about inheritance. Normally, constructors are called when an instance of a class is instantiated. Within that base class, however, constructors and destructors are NOT inherited like other members are, therefore, constructors for the derived classes need to be defined on their own.

class Animal {
public Animal() {
Console.WriteLine(“Animal created”);
}
~Animal() {
Console.WriteLine(“Animal deleted”);
Console.ReadLine();
}
}
class Dog: Animal {
public Dog() {
Console.WriteLine(“Dog created”);
}
~Dog() {
Console.WriteLine(“Dog deleted”);
Console.ReadLine();
}
}
class Program {
static void Main(string[] args) {
Dog d = new Dog();
Console.ReadLine();
}
}
  • While we then jump to the Dog class, since the Animal class is the base class, it’s constructor is automatically run and, as a result, Animal created is printed to the screen.
  • Then in the (derived) Dog class, the constructor prints Dog created to the screen.
  • With the program running, when I hit the Enter key, the next line of the Dog class is run, the destructor, which prints Dog deleted to the screen.
  • After that, when I tap the Enter key again, since the instance of the Dog class called d has been deleted, the destructor within the Animal class is run, and Animal deleted is printed to the screen.

POLYMORPHISM

class Shape {
public virtual void Draw() {
Console.Write(“Base Draw”);
}
}
class Shape {
public virtual void Draw() {
Console.Write(“Base Draw”);
}
}
class Circle : Shape {
public override void Draw() {
//draw a circle…
Console.WriteLine(“Circle Draw”);
}
}
class Rectangle : Shape {
public override void Draw() {
// draw rectangle…
Console.WriteLine(“Rect Draw”);
}
}
static void Main(string[] args) {
Shape c = new Circle();
c.Draw();
//Outputs “Circle Draw”
Shape r = new Rectangle();
r.Draw();
//Outputs “Rect Draw”
}
Shape c = new Circle();
…instead of what we’ve seen before…
Circle c = new Circle();
Shape c = new Circle();
Circle c = new Circle();