7 inheritance and polymorphism

Post on 08-May-2015

1.268 views 9 download

Transcript of 7 inheritance and polymorphism

Inheritance and Polymorphism

tnngo2@gmail.com

Programming in C#

Inheritance

Inheritance

Purpose of Inheritance

reuse common methods and attributes among classes without

recreating them

Generalization

Specialization

Extension

Implementing Inheritance

https://gist.github.com/2358793

protected

https://gist.github.com/2358827

based

access the variables and methods of the base class from the derived class

cannot use for invoking the static method

new

hide the methods or variables of the base class

https://gist.github.com/2358859

Constructor Inheritance

https://gist.github.com/2358859

Method overriding

Method Overriding allows the derived class to override or redefine the methods of the base class.

virtual & override virtual in base class

override in derived class

Question?

https://gist.github.com/2366263

Question?

https://gist.github.com/2366821

Sealed Classes

Sealed

prevent inheritance

https://gist.github.com/2366688

Purpose of Sealed Classes

Polymorphism

Overriding & Overloading

Compile-time & Run-time Polymorphism