Inheritance and Polymorphism
Base Classes
A derived class inherits members from a base class.
Base Classes
BaseClasses.cs
using System;
class Animal
{
public string Name { get; set; } = "";
public string Label()
{
return "animal:" + Name;
}
}
class Dog : Animal
{
}
class Program
{
static void Main()
{
string name = ;
Dog dog = new Dog();
dog.Name = name;
string label = dog.Label();
Console.WriteLine($"name={dog.Name}");
Console.WriteLine($"label={label}");
}
}
using System;
class Animal
{
public string Name { get; set; } = "";
public string Label()
{
return "animal:" + Name;
}
}
class Dog : Animal
{
}
class Program
{
static void Main()
{
string name = ;
Dog dog = new Dog();
dog.Name = name;
string label = dog.Label();
Console.WriteLine($"name={dog.Name}");
Console.WriteLine($"label={label}");
}
}
using System;
class Animal
{
public string Name { get; set; } = "";
public string Label()
{
return "animal:" + Name;
}
}
class Dog : Animal
{
}
class Program
{
static void Main()
{
string name = ;
Dog dog = new Dog();
dog.Name = name;
string label = dog.Label();
Console.WriteLine($"name={dog.Name}");
Console.WriteLine($"label={label}");
}
}
inheritance
Inheritance lets one class reuse behavior from another class.