Created
March 17, 2015 06:44
-
-
Save dnasca/d7d4db1a2cf5580f73c9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| using System; | |
| //This code isn't very practical, but it demonstrates the Child class inheriting from it's base class. Place a breakpoint at the program entry (Main()) to see the control flow. | |
| public class Parent //this class will be inherited by class Child | |
| { | |
| public Parent() | |
| { | |
| Console.WriteLine("Parent Constructor"); | |
| } | |
| public Parent(string myString) | |
| { | |
| Console.WriteLine(myString); | |
| } | |
| public void Print() | |
| { | |
| Console.WriteLine("I'm from the ParentClass"); //3rd & 5th output, even though it's invoked from child.print(), the base.print() points to the ParentClass | |
| } | |
| } | |
| public class Child : Parent | |
| { | |
| public Child() : base("From Derived Class") //1st output, string is passed into the Parent method | |
| { | |
| Console.WriteLine("Child Constructor."); //2nd output, from the Child method constructor | |
| } | |
| public new void Print() //hides method from class Parent | |
| { | |
| base.Print(); | |
| Console.WriteLine("I'm from the Child Class"); //4th output | |
| } | |
| public static void Main() | |
| { | |
| var child = new Child(); | |
| child.Print(); | |
| ((Parent)child).Print(); //cast child as type parent | |
| Console.ReadKey(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment