In Brief

This chapter looked at the important OOP topic of inheritance, the process of deriving one class from another. Although inheritance is familiar to OOP programmers, C# has its own takes on it. For example, structs are value types in C#, so they don't support inheritance. Here is an overview of the topics we discussed:

  • There are various access modifiers available for protecting the members of a class or struct; the default access is private . We can create new members with the new keyword, hiding the members with the same signature in the base class. We can also reach the hidden members using the base keyword. And we can use virtual methods designed to be overridden with the override keyword.

  • C# lets you create both abstract classes, whereby you need to derive new classes before you can create objects, and sealed classes, which you can't derive anything from.

  • C# supports polymorphism using virtual methods, and using polymorphism, you can load derived class objects into base class variables and call the methods of the derived class using that variable. With polymorphism, you can decide at runtime which object's method you want to call, without having to rewrite your code.

  • C# also supports boxing, unboxing, and nested classes. Boxing is the process of wrapping a value type in an object, whereas unboxing is the process of converting an object to a value type.

  • Interfaces let you specify a set of members that classes or structs that implement those interfaces must define. Interfaces act like contractsif you implement an interface, you agree to implement all the members of that interface.

  • C# also supports delegates, which work like function pointers in C++, giving you a form of polymorphism much like inheritance-based polymorphism. In C#, delegates are objects, not pointers, and are far safer.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net