code_junkie

Is there any way to call the parent version of an overridden method? (C# .NET)

Nov 13th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. class ParentClass
  2. {
  3. public int methodOne()
  4. {
  5. return methodTwo();
  6. }
  7.  
  8. virtual public int methodTwo()
  9. {
  10. return 1;
  11. }
  12. }
  13.  
  14. class ChildClass : ParentClass
  15. {
  16. override public int methodTwo()
  17. {
  18. return 2;
  19. }
  20. }
  21.  
  22. class Program
  23. {
  24. static void Main(string[] args)
  25. {
  26. var a = new ChildClass();
  27. Console.WriteLine("a.methodOne(): " + a.methodOne());
  28. Console.WriteLine("a.methodTwo(): " + a.methodTwo());
  29. Console.WriteLine("((ParentClass)a).methodTwo(): "
  30. + ((ParentClass)a).methodTwo());
  31. Console.ReadLine();
  32. }
  33. }
  34.  
  35. class ChildClass : ParentClass
  36. {
  37. new public int methodTwo() // bad, do not do
  38. {
  39. return 2;
  40. }
  41. }
Add Comment
Please, Sign In to add comment