C# OOPChapter 3 · C# Object Oriented Programming
Inheritance and polymorphism
Inheritance allows a class to derive from a base class, inheriting fields, properties, and methods. Use virtual in the base class and override in the subclass to redefine behavior.
Worked example
class SkyObject {
public virtual void Describe() {
Console.WriteLine("Object in sky");
}
}
class Star : SkyObject {
public override void Describe() {
Console.WriteLine("Bright star");
}
}How it reads
- class Star : SkyObject indicates that Star inherits from SkyObject
- virtual void Describe() allows subclasses to redefine this method
- override void Describe() redefines the method in the subclass

Cloud tip: C# only supports single inheritance for classes. A subclass can only inherit from one direct base class.


