Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
4. Inheritance
//4. Inheritance using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Inheritance is defining new types based on existing ones //New type is also the type it has inherited from var apple = new Apple(); apple.EatMe(); Console.WriteLine(apple.GetMyName()); Console.WriteLine((apple as Fruit).GetMyName()); Console.WriteLine(apple is Fruit); } } public class Fruit { public void EatMe() { Console.WriteLine("Eating..."); } public virtual string GetMyName() { return "Some fruit"; } } public class Apple: Fruit //Inherit from Fruit. Now Apple is a Fruit too. { public override string GetMyName() { return "Apple"; } } }
run
|
edit
|
history
|
help
0
test
StringBuilder
Student Cables
HDEFDCSA SS0546254
30272 Program Ex5 if_else2_odd_even
b
C# practice
3
CodeAdvent
simult 3x3