OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation...

Preview:

Citation preview

OOP

AbstractionClassesClass Members: Properties & MethodsInstance (object)EncapsulationInterfacesInheritanceCompositionPolymorphism

Using Inheritance Using Interfaces

Abstraction

A model of the properties, actions, and interactions of real world objects that are required for a software application.

Classes

A class is a template for an object. The class defines the properties (data attributes) and methods (functions in a class) that will be common to all instances of that class (objects) created from it.

Encapsulation

Functional details (how a member is implemented) of one object are hidden from objects that interact with it.

Classes & Interfaces

An Interface is a logical group of properties & methods that can be implemented by a class

• All Interface members are implicitly public• There is no code inside interface members. • Can’t be static, virtual, abstract, or sealed

Interfaces & ArcGIS

Once published, Interfaces should not be modified (add/remove members) (e.g. IBasicMap, IBasicMap2)

Inheritance

Some classes have some things in commonThe common things can be promoted to a base classThe specific things can remain in the derived classes

Inheritance syntax and keywords

MyClass : BaseClassMyClass inherits from BaseClass, e.g.:

<none | internal | public> class Cow : Animal

abstract keywordClass cannot be instantiated only derived from, e.g.:

public abstract class Animal

sealed keywordClass cannot be derived from, e.g.:

public sealed class Cow

Inheritance syntax and keywords

virtual & override keywordsBase class virtual members can be overridden in derived classes

Inheritance syntax and keywords

protected keyword

Member is accessible to base class and derived classes NOT to external classes

Inheritance syntax and keywords

base keyword

Call members on base class

Scope Review

public

internal (default)

protected

privateEntire solution

Assembly only

Derived classes

Class only

More …

More …

More …

More …

Inheritance and casting

Casting = converting between types

implicit

explicit

Can also cast between interfaces on • Same class• Base and derived classes

Abstract Classes vs Interfaces

SimilaritiesCan be

InheritedSomeClass : BaseClassISomeInterface : ISomeOtherInterface

Declared as variable typesBaseClass bc;ISomeInterface si;

Cannot be instantiatednew BaseClass()new ISomeInterface()

Abstract Classes vs Interfaces

Differences

Abstract Classes Interfaces

Inherit from one parentOne or more interfaces can be implemented on one or more classes

Abstract and non-abstract members

Members have no implementation

Members can be public, private, protected, internal, protected internal

Only public members

Composition

One class composed of another

Map Layer

FeatureLayer RasterLayer

*

No direct access to Udder via Cow

Composition can be setup to allowaccess to members of dependentclasses.

Composition in ArcGIS

Polymorphism (Using Inheritance)

Polymorphism: Members that have the same name but different implementations in different objects

Polymorphism using inheritance: Members in derived classes have same name but different implementations than the base class.

Polymorphism (Using Interfaces)

Implementation of interface can be different in classes that implement it

OOP

AbstractionClassesClass Members: Properties & MethodsInstance (object)EncapsulationInterfacesInheritanceCompositionPolymorphism

Using Inheritance Using Interfaces

Recommended