Inheritance C#


Published on

Published in: Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Inheritance C#

  1. 1. InheritanceTypes of inheritanceImplementing inheritanceInterface
  2. 2. TYPES OF INHERITANCE Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and functions. Interface inheritance means that a type inherits only the signatures of the functions and does not inherit any implementations.
  3. 3. IMPLEMENTATIONINHERITANCE A class derive from another class syntax:Class MyDerivedClass: MyBaseClass{ //function and data memebers here} A class derive from interface,the list of base class and interfaces is separated by commassyntax:Public class MyDerivedClass: MyBaseClass, Interface1,Interface2{//function and data memebers here}
  4. 4. VIRTUAL METHODS By declaring a base class function as virtual, you allow the function to be overridden in any derived classes. Class MyBaseClass { public virtual string VirtualMethod( ) { return “this method is virtural and defined in MyBaseClass” ;} } It is also permitted to declare a property as virtual. Public virtual string ForeName { get { return foreName;} set { foreName=value;} }
  5. 5. HIDING METHODS If a method with the same signature is declared in both base and derived classes, but the methods are not declared as virtual and override, respectively, then the derived class version is said to hide the base class version. Abstract classes and Functions: An abstract class cannot be instantiated,were as an abstract function does not have an implementation,and must be overriden in any non-abstract derived class Abstract function is automatically virtual.
  6. 6. SEALED CLASSES ANDMETHODS C# allows classes and methods to be declared as sealed. In case of class this means that you can’t inherit from that class syntax: sealed class Class_Name { // etc } In case of method syntax: class MyClass: MyclassBase { public sealed override void FinalMethod() { // etc } } class DerivedClass: MyClass { public override void FinalMethod() // wrong..will give compilation error { // etc } }
  7. 7. Constructors of Derived Classes Adding a constructor in a Hierarchy Adding a constructor with parameters to a Hierarchy
  8. 8. MODIFIERS  Modifiers can indicate the visibility of a methods.MODIFIER APPLIES TO DESCRIPTIONpublic Any type or members The item is visible to any other code.protected Any member of a type, also The item is visible only to any nested type any derived type.internal Any member of a type, also The item is visible only with any nested type in its containing assembley.private Any types or memebers The item is visible only inside the type to which it belongsprotected internal Any member of a type, also The item is visible to any any nested type code within its containing assembly and also to any code inside a derived type
  9. 9. MODIFIER APPLIES TO DESCRIPTIONnew Function members The member hides an inherited member with the same signaturestatic All members The member does not operate on a specific instance of the classvirtual Function members only The member can be overridden by a derived classabstract Function members only A virtual member that defines the signature of the member, but doesn’t provide an implementation.override Function members only The member overrides an inherited virtual or abstract membersealed Classes, methods, and class cannot be inherited, member properties overrides an inherited virtual member, but cannot be overridden by any members in any derived classesextern static [DllImport] The member is implemented methods only externally, in a different language.
  10. 10. INTERFACESsyntax: public interface Interface_Name { void method(); } You can never instantiate an interface, it contains only the signatures of its members. An interface has neither constructors nor fields and also not allowed to contain operator overloads. An interface members are always implicitly public, and cannot be declared as virtual or static.