2124 module 10

Created Tuesday 10 December 2013

Inheritance in C#

  1. In object oriented system inheritance is the ability of an object to inherit data and functionality from its parent object.
  2. A child object can substitute for the parent object.
  3. Using inheritance it's possible to create new classes from existing classes instead of writing them from scratch, and then write a new code to add the features required by the new class.
  4. The parent class is known as a base class, and the child class is known as a derived class.
  5. Inheritance is a code reuse mechanism.
  6. Inheritance is a type-classification mechanism, and it is more important.
  7. The base class should be designed to enable inheritance.
  8. Objects must have the proper structure or the inheritance won't work properly.
  9. If a poorly designed base class changes then its child may become inoperable.
  10. A derived class inherits everything from the base class except for the base class constructors and destructors.
  11. Public members of the base class are implicitly public memblers of the derived class.
  12. Private members of the base class are inherited by the derived class but only accessible only to the members of the base class.
  13. A derived class cannot be more accessible than its base class. Private class <- public class == error.
  14. In C# all inheritance is public. C++ allows implicit private inheritance.
  15. When a derived class iherits a protected member, this member also is implicitly a protected member of the derived class.
  16. Members of a derived class can only access their own inherited protected members. They cannot access the protected members of the base class through references to the base class.
  17. Many guidelines recommend keeping all data private and using protected access only for methods.
  18. A struct doesn't support inheritance.
  19. If the derived class doesn't explicitly call a base class constructor, the C# compiler will implicitly use a constructor initializer of the form : base().
  20. If a class provides an explicit constructor of its own, the compiler won't create a default constructor. But the specified constructor must match some constructor of the base class or compile-time error will be to come.
  21. If a base class constructor is private, then a derived class can't access it.
  22. Virtual method must contain a method body.
  23. Virtual methods can't be private (accessibility) or static (polymorphism works on objects).
  24. Override method must have a method body
  25. An override method is implicitly virtual, and cannot be set virtual explicitly
  26. Hiding is for methods with identical signatures
  27. It's possible to use 'new' for hiding fields and nested classes
  28. Since sealed classes never will be inherited it's possible to make virtual member calls to non-virtual for run-time optimizations