18
Abstract classes & Interfaces [email protected] Programming in C#

8 abstract classes and interfaces

Embed Size (px)

Citation preview

Page 1: 8   abstract classes and interfaces

Abstract classes & Interfaces

[email protected]

Programming in C#

Page 2: 8   abstract classes and interfaces

Abstract Classes

Page 3: 8   abstract classes and interfaces

Abstract Classes

referred to as an incomplete base class

cannot be instantiated, can only be sub-classed.

Page 4: 8   abstract classes and interfaces

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

https://gist.github.com/2373593

Page 5: 8   abstract classes and interfaces

Inheritance

Page 6: 8   abstract classes and interfaces

Purpose

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

Page 7: 8   abstract classes and interfaces

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

Page 8: 8   abstract classes and interfaces

Implemetation

https://gist.github.com/e63d1cf32b03cd888686

Page 9: 8   abstract classes and interfaces

Mutiple Inheritance

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

Page 10: 8   abstract classes and interfaces

Interface Inheritance

An interface can inherit multiple interfaces but cannot implement

them.

https://gist.github.com/2374963

Page 11: 8   abstract classes and interfaces

What difference between abstract classes & Interfaces?

Page 12: 8   abstract classes and 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

Page 13: 8   abstract classes and interfaces

Similarities

cannot be instantiated

are implemented by the inheriting subclass

can inherit multiple interfaces

Page 14: 8   abstract classes and interfaces

Differences

Page 15: 8   abstract classes and interfaces

Some stories

Page 16: 8   abstract classes and interfaces
Page 17: 8   abstract classes and interfaces

Recommendations

Page 18: 8   abstract classes and interfaces

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