CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

More on Classes Inheritance and Polymorphism
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CET203 SOFTWARE DEVELOPMENT Session 1B Modelling and the Theory of Inheritance.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Writing Classes (Chapter 4)
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
CET203 SOFTWARE DEVELOPMENT Session 3A Abstract classes and Polymorphism.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
M1G Introduction to Programming 2 5. Completing the program.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Fusion Design Overview Object Interaction Graph Visibility Graph Class Descriptions Inheritance Graphs Fusion: Design The overall goal of Design is to.
OOP Basics Classes & Methods (c) IDMS/SQL News
Module 9. Dealing with Generalization Course: Refactoring.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Lecture 12 Inheritance.
03/10/14 Inheritance-2.
Packages and Interfaces
Week 6 Object-Oriented Programming (2): Polymorphism
Two big words Inheritance Polymorphism
Java Inheritance.
Review of Previous Lesson
Presentation transcript:

CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

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

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.

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

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()

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

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

Exercise 1b Suggest a class diagram for the Ticket class

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.

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.

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.

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

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.

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.

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

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...] }

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...] }

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

The new class diagram

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); }

Exercise 3 Publications2C.sln

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

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!

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.

Exercise 4 Publications2D.sln

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

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"); }

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.