Inheritance C#

11
Inheritance Types of inheritance Implementing inheritance Interface

description

 

Transcript of Inheritance C#

Page 1: Inheritance C#

Inheritance

Types of inheritanceImplementing inheritanceInterface

Page 2: Inheritance C#

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.

Page 3: Inheritance C#

IMPLEMENTATION INHERITANCE 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 commas

syntax:Public class MyDerivedClass: MyBaseClass,

Interface1,Interface2{//function and data memebers here}

Page 4: Inheritance C#

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;} }

Page 5: Inheritance C#

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.

Page 6: Inheritance C#

SEALED CLASSES AND METHODS 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 } }

Page 7: Inheritance C#

Constructors of Derived Classes

Adding a constructor in a Hierarchy

Adding a constructor with parameters to a Hierarchy

Page 8: Inheritance C#

MODIFIERS Modifiers can indicate the visibility of a methods.

MODIFIER APPLIES TO DESCRIPTION

public Any type or members The item is visible to any other code.

protected Any member of a type, also any nested type

The item is visible only to any derived type.

internal Any member of a type, also any nested type

The item is visible only with in its containing assembley.

private Any types or memebers The item is visible only inside the type to which it belongs

protected internal Any member of a type, also any nested type

The item is visible to any code within its containing assembly and also to any code inside a derived type

Page 9: Inheritance C#

MODIFIER APPLIES TO DESCRIPTION

new Function members The member hides an inherited member with the same signature

static All members The member does not operate on a specific instance of the class

virtual Function members only The member can be overridden by a derived class

abstract 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 member

sealed Classes, methods, and properties

class cannot be inherited, member overrides an inherited virtual member, but cannot be overridden by any members in any derived classes

extern static [DllImport] methods only

The member is implemented externally, in a different language.

Page 10: Inheritance C#

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.

Page 11: Inheritance C#