19
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance Using Interfaces

OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Embed Size (px)

Citation preview

Page 1: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

OOP

AbstractionClassesClass Members: Properties & MethodsInstance (object)EncapsulationInterfacesInheritanceCompositionPolymorphism

Using Inheritance Using Interfaces

Page 2: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Abstraction

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

Page 3: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

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.

Page 4: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Encapsulation

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

Page 5: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

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

Page 6: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Interfaces & ArcGIS

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

Page 7: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

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

Page 8: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

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

Page 9: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Inheritance syntax and keywords

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

Page 10: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Inheritance syntax and keywords

protected keyword

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

Page 11: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Inheritance syntax and keywords

base keyword

Call members on base class

Page 12: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Scope Review

public

internal (default)

protected

privateEntire solution

Assembly only

Derived classes

Class only

More …

More …

More …

More …

Page 13: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Inheritance and casting

Casting = converting between types

implicit

explicit

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

Page 14: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Abstract Classes vs Interfaces

SimilaritiesCan be

InheritedSomeClass : BaseClassISomeInterface : ISomeOtherInterface

Declared as variable typesBaseClass bc;ISomeInterface si;

Cannot be instantiatednew BaseClass()new ISomeInterface()

Page 15: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

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

Page 16: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

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

Page 17: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

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.

Page 18: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

Polymorphism (Using Interfaces)

Implementation of interface can be different in classes that implement it

Page 19: OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance

OOP

AbstractionClassesClass Members: Properties & MethodsInstance (object)EncapsulationInterfacesInheritanceCompositionPolymorphism

Using Inheritance Using Interfaces