8 abstract classes and interfaces

Post on 10-May-2015

2.029 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Abstract classes & Interfaces

tnngo2@gmail.com

Programming in C#

Abstract Classes

Abstract Classes

referred to as an incomplete base class

cannot be instantiated, can only be sub-classed.

Implementation The subclass inheriting the abstract class has to override and implement the abstract methods

https://gist.github.com/2373593

Inheritance

Purpose

A subclass in C# cannot inherit two or more base class.

Interfaces

contains only abstract members

cannot implement any methods

cannot be instantiated

by default, all members have public as the access modifier

cannot contain constants, data fields, constructors, destructor and static members

Implemetation

https://gist.github.com/e63d1cf32b03cd888686

Mutiple Inheritance

https://gist.github.com/7856dad9ccfe58aaa0da

Interface Inheritance

An interface can inherit multiple interfaces but cannot implement

them.

https://gist.github.com/2374963

What difference between abstract classes & Interfaces?

Short

Interface: contract only, no implementation, no instantiation

Abstract class: contract, some implementation, no instantiation

Interface specifying what the object can do

Abstract specify what an object is

Similarities

cannot be instantiated

are implemented by the inheriting subclass

can inherit multiple interfaces

Differences

Some stories

Recommendations

Multiple version of component => Abstract class

functionality will be useful across a wide range of disparate objects

=> Interfaces

small, concise function bits of functionality => Interfaces

common, implemented functionality among all implementations of your component => Abstract class

top related