Created
May 23, 2024 14:25
-
-
Save ChrisMoney/61af27068901bfb76ac4ae2e8d4448d5 to your computer and use it in GitHub Desktop.
Abstract Class in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Abstract class | |
abstract class Animal | |
{ | |
// Abstract method (does not have a body) | |
public abstract void animalSound(); | |
// Regular method | |
public void sleep() | |
{ | |
Console.WriteLine("Zzz"); | |
} | |
} | |
// Derived class (inherit from Animal) | |
class Pig : Animal | |
{ | |
public override void animalSound() | |
{ | |
// The body of animalSound() is provided here | |
Console.WriteLine("The pig says: wee wee"); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Pig myPig = new Pig(); // Create a Pig object | |
myPig.animalSound(); // Call the abstract method | |
myPig.sleep(); // Call the regular method | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment