Sunteți pe pagina 1din 15

Types of inheritance All classes are ultimately derived from the class System.

.Object There are two distinct types of inheritance Implementation inheritance a derived type adopts the base type s implementation of each function Interface inheritance inherits only the signatures of the functions and does not inherit any implementations

Multiple Inheritance a class derives from more than one other class C# class can be derived from one other class, and any number of interfaces Structs and Classes Structs are always derived from System.ValueType Structs don t really support implementation inheritance, but they do support interface inheritance public struct MyDerivedStruct : IInterface1, IInterface2 { // etc. }

Implementation Inheritance a class derives from another class class MyDerivedClass : MyBaseClass { // functions and data members here } a class (or a struct) also derives from interfaces public class MyDerivedClass : MyBaseClass, IInterface1, IInterface2 { // etc. }

the C# compiler will assume that System.Object is the base class class MyClass : Object // derives from System.Object { // etc. } class MyClass // derives from System.Object { // etc. } C# supports the object keyword, which serves as a pseudonym for the System.Object class, class MyClass : object // derives from System.Object { // etc. }

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 virtual and defined in MyBaseClass; } } class MyDerivedClass : MyBaseClass { public override string VirtualMethod() { return This method is an override defined in MyDerivedClass.; } }

declare a property as virtual


public virtual string ForeName { get { return fName;} set { fName = value;} } private string foreName;

C#, functions are not virtual by default

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
class MyDerivedClass: HisBaseClass { public int MyGroovyMethod() { // some groovy implementation return 0; } } class MyDerivedClass : HisBaseClass { public new int MyGroovyMethod() { // some groovy implementation return 0; } }

Calling Base Versions of Functions calling base versions of a method from a derived class: base. < MethodName > ()
class CustomerAccount { public virtual decimal CalculatePrice() { // implementation return 0.0M; } } class GoldAccount : CustomerAccount { public override decimal CalculatePrice() { return base.CalculatePrice() * 0.9M; } }

Abstract Classes and Functions An abstract class cannot be instantiated Abstract function does not have an implementation Must be overridden in any non - abstract derived class An abstract function is automatically virtual If any class contains any abstract functions, that class is also abstract abstract class Building { private bool damaged = false; // field public abstract decimal CalculateHeatingCost(); // abstract method }

Sealed Classes and Methods Sealed class, can t be inherit Sealed method, can t be override
sealed class FinalClass { // etc } class DerivedClass : FinalClass // wrong. Will give compilation error { // etc } class MyClass { public sealed override void FinalMethod() { // etc. } } class DerivedClass : MyClass { public override void FinalMethod() // wrong. Will give compilation error { } }

Constructors of Derived Classes


abstract class GenericCustomer { private string name; public GenericCustomer(string name) { this.name = name; } } class NevermoreCustomer : GenericCustomer { public NevermoreCustomer(string name): base(name) { } } NevermoreCustomer customer = new NevermoreCustomer(Arabel Jones);

class NevermoreCustomer : GenericCustomer { public NevermoreCustomer(string name, string referrerName):base(name) { this.referrerName = referrerName; } private string referrerName; } NevermoreCustomer customer = new NevermoreCustomer(Arabel,Jones);

Visibility Modifiers

Keyword Private Protected Internal Protected Internal Public

Containing Classes

Derived Classes

Containing Program

Anywhere Outside

Interfaces
public interface Addition { void Add(); } class SomeClass : Addition { // This class MUST contain an implementation of //the Addition.Add() method, otherwise //you get a compilation error. public void Add() { // implementation of Add() method } // rest of class }

Addition inObj = new SomeClass (); inObj.Add(); (or) SomeClass sclassObj= new SomeClass(); Addition inObj = (Addition)classObj; (or) Addition inObj = classObj as Addition; inObj.Add();

Derived Interfaces
public interface Compute : Addition { void Sub(); }

Multiple Interfaces
public interface Addition { void Add(); } public interface Subtraction { void Sub(); } class Computation : Addition, Subtraction { public void Add(){ } public void Sub(){ } }

S-ar putea să vă placă și