Lecture overview -- Keyboard shortcut: 'u'  Previous page: Inheritance of methods: Example. -- Keyboard shortcut: 'p'  Next page: Polymorphism. Static and dynamic types -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 30 : 40
Object-oriented Programming in C#
Specialization, Extension, and Inheritance
Overriding and Hiding in C#

What if a method M in class A also appears in class B?

class A {
  public void M(){}
}

class B: A{
  public void M(){}
}

Two methods M in classes A and B, where B inherits from A.

  • Intended redefinition:
    B.M is intended to redefine A.M - such that B.M is used on B instances

    • A.M must be declared as virtual

    • B.M must be declared to override A.M

  • Accidental redefinition:
    The programmer of class B is not aware of A.M

    • B.M must declare that it is not related to A.M - using the new modifier