Presentation is loading. Please wait.

Presentation is loading. Please wait.

CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces.

Similar presentations


Presentation on theme: "CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces."— Presentation transcript:

1 CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

2 Objectives Define interfaces to extend polymorphism beyond inheritance hierarchies Appreciate the scope for extensibility which polymorphism provides

3 Inheritance There are two aspects to inheritance: – the subclass inherits the interface (i.e. access to public members) of its superclass – this makes polymorphism possible – the subclass inherits the implementation of its superclass (i.e. instance variables and method implementations) – this saves us copying the superclass details in the subclass definition In C#, the : keyword automatically applies both these aspects.

4 Interfaces Sometimes we may want two classes to share a common interface without putting them in an inheritance hierarchy. This might be because – they aren’t really related by a true ‘is a’ relationship – we want a class to have interfaces shared with more than one would-be superclass, but C# does not allow such ‘multiple inheritance’ – we want to create a ‘plug and socket’ arrangement between software components, some of which might not even be created at the current time

5 Revision Book Author : String OrderCopies(copies : int) Magazine orderQty : int currIssue : String AdjustQty(quantity : int) RecvNewIssue(newIssue : String) DiscMag RecvNewIssue(newIssue : String) CashTill runningTotal : double SellItem(pub : Publication) ShowTotal() > Publication title : String price : double copies : int SellCopy()

6 Tickets In addition to books and magazines, we now want to accommodate selling tickets, e.g. for entertainment events, public transport, etc. These are not like Publications because – we don’t have a finite ‘stock’ but print them on demand at the till – tickets consist simply of a description, price and client (for whom they are being sold) – these sales are really a service rather than a product

7 Exercise 1a What features do Tickets have in common with Publications?

8 Exercise 1b Suggest a class diagram for the Ticket class

9 SellCopy() for Ticket public void SellCopy() { Console.WriteLine("**************************************"); Console.WriteLine(" TICKET VOUCHER "); Console.WriteLine(this.ToString()); Console.WriteLine("**************************************"); Console.WriteLine(); } This is quite different to – and not simply a modification of – the behaviour involved in selling a Publication. The ticket details are simply ‘printed’ in the system console window using ToString(). In particular note the absence of any stock control functionality because we don’t have a ‘stock’ of tickets.

10 To the CashTill We don’t feel that Ticket belongs in an inheritance hierarchy with Publications. But we do want to be able to check tickets through the till as we can with publications. Just like publications, tickets provide the operations which CashTill needs: – SellCopy() – Price (the property provides the operation of obtaining the price) We will define this set of operations as an ‘Interface’ called ISaleableItem.

11 ISaleableItem Interface The ISaleableItem interface will capture the small amount of functionality – just two operations – which are needed for the CashTill to process an item. Any class which implements this interface will then be acceptable to the till. Note that we have included the property Price. The get and/or set methods within a property can also form part of the interface.

12 ISaleableItem interface ISaleableItem { double Price { get; } void SellCopy(); }

13 Interface definition The interface defines purely the signatures of operations without their implementations. All the methods are implicitly public so this is not stated, and there can be no instance variables or constructors. An interface defines the availability of specified operations without saying anything about their implementation. That is left to classes which implement the interface. In this example we have provided the get operation for the property price but not the set operation since the CashTill is only interested in selling the items, not changing their prices.

14 Contract An interface is a sort of contract. The ISaleableItem interface says “I undertake to provide, at least, methods with these signatures: double Price{get;} void SellCopy(); though I might include other things as well” Where more than one class implements an interface it provides a guaranteed area of commonality which polymorphism can exploit.

15 Exercise 2 Think of a car and a driving game in an arcade. They certainly are not related by any “is a” relationship – they are entirely different kinds of things, one a vehicle, the other an arcade game. They do share some common operations however Suggest an interface that they could both implement Note: there may be a number of interfaces that could be suggested

16 Implementing an interface We now need to state that both Publication (and all its subclasses) and Ticket both offer the operations defined by this interface: class Publication : ISaleableItem { [...class details...] } class Ticket : ISaleableItem { [...class details...] }

17 Inheritance & Interfaces It is possible for a class in C# to inherit from a single superclass and to implement one or more interfaces This is denoted using one : and commas between the superclass and interfaces This is one way of getting around the lack of multiple inheritance in C#! class MyClass : MySuperClass, IMyInterface { [...class details...] }

18 Polymorphic CashTill The CashTill class already employs polymorphism: the SellItem method accepts a parameter of type Publication which allows any of its subclasses to be passed: public void SellItem (Publication pub) We now want to broaden this further by accepting anything which implements the SaleableItem interface: public void SellItem (ISaleableItem selb) When the type of a variable or parameter is defined as an interface, this works just like a superclass type. Any class which implements the interface is acceptable for assignment to the variable/parameter because the interface is a type and all classes implementing it are subtypes of that type

19 The new class diagram

20 Code for CashTill.SellItem() public void SellItem(ISaleableItem selb) { String msg; runningTotal = runningTotal + selb.Price; selb.SellCopy(); msg = "Sold " + selb.ToString() + " @ " + selb.Price + "\nSubtotal = " + runningTotal; Console.WriteLine(msg); }

21 Exercise 3 Publications2C.sln

22 Exercise 3 Assuming the following objects have been declared: CashTill myTill = new CashTill(); Book book1 = new Book("Software Development", 10.99, 3, "Liz Gandy"); Magazine mag1 = new Magazine("C# monthly", 2.5, 10, 10, "September"); DiscMag dmag1 = new DiscMag("C# programming", 2.5, 10, 10, "September"); Ticket ticket1 = new Ticket("The Sound of Music", 25.99, "Sunderland Empire"); Write the code statements to sell one copy of each item through the new CashTill and then show the overall total

23 Extensibility Polymorphism allows objects to be handled without regard for their precise class. This can assist in making systems extensible without compromising the encapsulation of the existing design. For example, we could create new classes for more products or services and so long as they implement the SaleableItem interface the CashTill will be able to process them without a single change to its code!

24 Distinguishing subclasses What if we have an object handled polymorphically but need to check which subtype it actually is? The is operator can do this: object is class This test is true if the object is of the specified class (or a subclass), false otherwise.

25 Exercise 4 Publications2D.sln

26 Exercise 4 Consider the following objects: Ticket myTicket; Magazine myMag; DiscMag myDiscMag; Which of the following will be true or false: myDiscMag is DiscMag myTicket is Publication myDiscMag is Magazine myMag is DiscMag myDiscMag is Publication myTicket is ISaleableItem

27 Using “is” public void SaleType(ISaleableItem selb) { if (selb is Publication) { Console.WriteLine("This is a Publication"); } else if (selb is Ticket) { Console.WriteLine("This is a Ticket"); } else { Console.WriteLine("This is a an unknown sale type"); }

28 Summary Sometimes we want to employ polymorphism without all the classes concerned having to be in an inheritance hierarchy. The ‘interface’ construct allows us to provide shared interfaces (i.e. collections of operations) in this situation. There is no inherited implementation – the class must implement ALL the operations itself as appropriate. Any number of classes can implement a particular interface. If necessary we can determine the type of an object which is referenced polymorphically.


Download ppt "CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces."

Similar presentations


Ads by Google