Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 3: Component/Object Oriented Programming, Events, and Delegates in.NET Jim Fiddelke, Greenbrier & Russel.

Similar presentations


Presentation on theme: "Section 3: Component/Object Oriented Programming, Events, and Delegates in.NET Jim Fiddelke, Greenbrier & Russel."— Presentation transcript:

1 Section 3: Component/Object Oriented Programming, Events, and Delegates in.NET Jim Fiddelke, Greenbrier & Russel

2 Section 3: Component/Object Oriented Programming with.NET Component-Oriented Versus Object-Oriented Programming Encapsulation Polymorphism Inheritance Overloading Interfaces Events Delegates

3 Component-Oriented Versus Object-Oriented Programming Component is an overused term Unlike traditional object-oriented classes, every.NET class is a binary component. Therfore our defintion of a component is : “a component is a.NET class” A component is responsible for exposing business logic to an entity that uses the component (the “client”) While.NET doesn’t enforce interface-based programming, you should strive to do so whenever possible. Component-Oriented vs. Object-Oriented programming really boils down to creating building blocks vs. monolithic applications. The contracts that define how the building blocks are to interact are the interfaces. –component programming emphasizes interfaces –object-oriented emphasizes inheritance – poor way to reuse.

4 Component-Oriented Versus Object-Oriented Programming To use a component the client needs to know only the interface definition and be able to access a binary component that implements that interfaces (the “server”). Why: scalability, availability, throughput Emphasis on interfaces An interface is simply a contract for behavior that defines the members a class should implement. It does not specify details on how it should be implemented. More on interfaces later

5 Components provide “location transparency” – there is nothing in the client’s code pertaining to where the object executes. Machine A Process 1 Object Client Process 2 Object Machine B Process 1 Object WWW WWW Machine C Process 1 Object

6 Recall… –Classes are blueprints for objects –Objects are composed of members –Members include: data –fields –properties methods – actions the object can perform events – notifications an object receives or sends

7 Polymorphism “Polymorphism is the ability of different classes to provide different implementations of the same public interfaces.”

8 Encapsulation “One of the most important principles of objects is that they enclose functionality and data while providing an interface with which to interact with other objects. An object's users don't care how the object does what it does; they just want it done well and to have a simple interface. This "black box" characteristic of objects is known as encapsulation. “

9 Overloading Members Members include: –data fields properties –methods – actions the object can perform –events – notifications an object receives or sends Overloading allows you to create multiple members with the same name Each member that shares a name must have a different signature Most common in methods. C# allows operator overloading.

10 Overloading: let’s talk about inheritance first… Both C# and VB.NET can use class-based inheritance In.NET a class can derive from at most one other class However, a class can implement multiple interfaces

11 Class-based Inheritance Class-based Inheritance C#: class Account { //some code } class Checking : Account { //add some functionality to base.Account } VB.NET: Class Account ‘some code End Class Class Checking Inherits Account ‘ Add some functionality to MyBase.Account End Class

12 public class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); } public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); } Class-based Inheritance Class-based Inheritance

13 Class Inheritance Demo

14 Overloading Again, “Overloading means that several methods have the same name but different signatures”… C# class Account { public void MakeDeposit(int Amount) { } public void MakeDeposit(int Amount, int AmountAvailable) { } } VB.NET Class Account Sub MakeDeposit(ByVal Amount As Integer) End Sub Sub MakeDeposit(ByVal Amount As Integer, _ ByVal AmountAvailable As Integer) End Sub End Class

15 Overloading Both C# and VB.NET have a mechanism by which you can mark a function as virtual in the base class, and then override the method in the derived class. class Account { public virtual void MakeDeposit(int Amount) { } class Checking : Account { public override void MakeDeposit(int Amount) { } In VB.NET: virtual = Overridable override = Overrides

16 class Account { public virtual void MakeDeposit(int Amount) { } class Checking : Account { public override sealed void MakeDeposit(int Amount) { } class SuperChecking : Checking { //*** this is illegal *** public override void MakeDeposit(int Amount) { } In VB.NET: sealed = NotOverridable And you can also mark a class (or member) as “sealed”- it cannot be used as a base class…

17 What about the opposite – you want to create a class or member that you must inherit from… abstract class Account { public void ReportInfo() { System.Console.WriteLine( “Account is of type “ + AccountType); } public abstract string AccountType { get;}; //“ReadOnly” in VB.NET } class Checking : Account { public override string AccountType { get { return “Checking”; } VB.NET: class abstract = MustInherit property abstract = MustOverride override = Overrides

18 Interfaces interface IParentInterface { void ParentInterfaceMethod(); } interface IMyInterface : IParentInterface { void MethodToImplement(); } class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); iImp.ParentInterfaceMethod(); } public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); } public void ParentInterfaceMethod() { Console.WriteLine("ParentInterfaceMethod() called."); }

19 Interfaces Demo

20 Class Projects - feel free to work with your neighbor -

21

22

23

24

25

26

27


Download ppt "Section 3: Component/Object Oriented Programming, Events, and Delegates in.NET Jim Fiddelke, Greenbrier & Russel."

Similar presentations


Ads by Google