presented by ted higgins, sql server dba an introduction to object oriented programming

Download Presented by Ted Higgins, SQL Server DBA An Introduction to Object  Oriented Programming

If you can't read please download the document

Upload: jodie-dixon

Post on 18-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

Evolution of Programming Languages

TRANSCRIPT

Presented by Ted Higgins, SQL Server DBA An Introduction to Object Oriented Programming A high-level overview of Object Oriented Programming Code demos of the concepts again high-level, basic stuff Some of the benefits of OOA/OOP Why it may, or may not, be for you: the C or COBOL developer Ill try and stay on track with the concept of OOP design and what it is versus concentrating on the OO language C# in this case Code examples are not designed for production! While I have attempted to present the code demos in a clear and concise manner, they are just that, demos! Introduction Evolution of Programming Languages C# Application Creation C# Application Execution The.NET Framework What is OOP? Key Concepts of OOP Concepts We Will Explore What is an Object A Simple Example Object vs. Procedural: What is a Class A Class defines an Object A Class has Fields/Properties that define it (its State) A Class has Methods that define its behavior Vehicle Lets add some fields to our Vehicle class private int vehicleID private string propulsion Behavior is handled by Methods Properties set and retrieve the private data in the class (note: private and public are part of encapsulation) public vehicle() public vehicle(x, y) VehicleID {get; set;} Propulsion {get; set;} Virtual void GetValues( ); Constructors are used to initialize the Object at creation time Coding a (Base) Class Lets code a class in C# 1.Create and name the class 2.Create its members 3.Create a custom constructor 4.Create methods 5.Create Properties public class Vehicle { private int _vehicleID; private string _propulsion; public Vehicle (ID, propulsion) { this._vehicleID = ID; this._propulsion = propulsion; } public virtual string GetValues () { Console.Writeline(ID: {0}, Propulsion: {1}, _vehicleID, _propulsion ); } public int VehicleID { get { return _vehicleID; } set { _vehicleID = value; } } } // end class Vehicle Instantiating an Object Lets create a Vehicle object in C# 1.Create a reference variable of type 2.Instantiate (create) the object using the new keyword 3.Access fields, properties and methods using the dot operator Vehicle myCar; myCar = new Vehicle( ); myCar.VehicleID = 2; myCar.Propulsion = Turbo V6; myCar.GetInformation( ); The default constructor for the Vehicle class is called here Encapsulation Defined Encapsulation keeps private properties from being accessed by Objects created outside of the class Private, Protected, and Public are access modifiers Private members may only be accessed by Objects of the class Public members may be accessed by Objects outside of the class Vehicle private vehicleID private propulsion public vehicle() public vehicle(x, y) VehicleID {get; set;} Propulsion {get; set;} void GetValues ( ) Inheritance Defined Inheritance allows us to build a Class/Object Hierarchy We start by designing a Base Class, with Properties, Fields and Methods A Class may then be derived from the Base Class, Inherits all of that Classes members, and may add additional members as necessary to refine the object Vehicle Automobile Airplane Is-A -vehicleID - propulsion -vehicleID - propulsion -vehicleID - propulsion Base Class Derived Classes Inheritance is always based upon an Is-A relationship between base and derived classes Inheritance Code Example The preceding Class would be coded in C# like this: Base Class - Vehicle Private Fields Public Method calls Private Methods Private Methods to set Private Fields Derived Class: Automobile Derived Class: Airplane Inheritance Code Example 2 - Properties Base Class - Vehicle Private Fields Constructors Properties to set Private Fields Virtual Method may be overridden by a derived class Polymorphism Defined Polymorphism allows a Class to be used as more than one type: Its defined type, Base type, or Interface type Overriding and Overloading of methods are used to implement polymorphism The correct method(s) are for the particular Object type are chosen at runtime Vehicle Automobile Airplane -vehicleID -propulsion + GetValues() -vehicleID -propulsion + GetValues() -vehicleID -propulsion + GetValues() Base Class Derived Classes The inherited GetValues method will be overridden by each derived class. The CLR will choose the correct implementation at runtime. Polymorphism Implementation Polymorphism depends on Inheritance, you must use Inheritance to implement polymorphism Polymorphism is normally accomplished using one of the following: Abstract Classes Interfaces Characteristics of Abstract Classes: An abstract class must be marked as abstract You may not instantiate an abstract class An Abstract method has no implementation, the derived class must implement If you mark a method within a class as abstract, the entire class must be abstract All abstract methods within a abstract class must be implemented by the derived class An abstract method is automatically virtual, so that it may be overridden Characteristics of an Interface: An interface is declared using the interface keyword The interface provides only the return type and method name, no implementation All methods are inherently public, no access modifiers are allowed Interfaces can specify the signature for methods, properties and indexers Interfaces cannot define data members, constructors, or destructors No member can be declared static All members of an Interface must be implemented by the derived class Advanced C# Techniques Design Patterns UML Unified Modeling Language Advanced C# Techniques Some advanced C# (as well as other languages) techniques: Delegates Indexers Generics Nullable Types Interfaces Multiple Inheritance Check out this free.pdf booklet on C# and.NET from Charles Petzold: Thank You!