1 / 19

INTRO TO OBJECT ORIENTED PROGRAMMING –

This review introduces the concept of objects and the limitations of using classes in object-oriented programming. It discusses the use of objects in a simple application and demonstrates the concepts of message passing and method invocation.

brunson
Download Presentation

INTRO TO OBJECT ORIENTED PROGRAMMING –

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INTRO TO OBJECT ORIENTED PROGRAMMING – Objects (Getting rid of static) Pete Collingwood

  2. Review: • Introduced the idea of a Class. • Mechanism for structuring code • Required support to reference elements in the class <ClassName>.<methodCall( )> Needs relevant methods or variables to be public static. • Also required support so compiler can find Class (“physical”location) WHY DON’T WE TALK ABOUT CLASS ORIENTED DEVELOPMENT? Pete Collingwood

  3. OtherClasses MessageStore EmailClient Message attributes attributes message_store attributes message_store attributes to_field from_field subject_field methods sendMessage methods endMessage methods methods setToField setFromField getToField getFromField Why not just Classes? Think about what’s likely to happen in a simple Email system: more than one message more than one client?? Pete Collingwood

  4. Objectives for this session: • address this limitation of the use of Classes by • Introducing the notion of an Object. • Show how this extends and refines the concept of a class. • Demonstrate the use of objects in a simple application. • Introduce and illustrate the concepts of message passing and method invocation. Pete Collingwood

  5. Objects – another view of Classes • From the simple Email System example, it is clear that we want to able to make “copies” of a class – ie to be able to use the “structure” of attributes and methods defined in a class. • We have (implicitly) encountered this situation before. When we declare variables, eg: int my_int; float my_float; Behind the scenes, memory is allocated to each of these variables. Further, we also have access to certain operations associated with each of these data items…. inbuilt operations +,/,-, * Pete Collingwood

  6. Objects as variables • In fact Classes are used in just the same way: they are (user-defined) datatypes. • So, we can define variables of <Classname> type eg: Console my_console; Message my_message; EmailClient this_client; • Such variables are referred to as: objects, (of type<Classname>) or instances (of the <Classname> Pete Collingwood

  7. my_message my_message Message my_message attributes to_field from_field subject_field attributes to_field from_field subject_field attributes to_field from_field subject_field attributes to_field from_field subject_field methods setToField setFromField getToField getFromField methods setToField setFromField getToField getFromField methods setToField setFromField getToField getFromField methods setToField setFromField getToField getFromField Objects as variables – the idea • Objects are copies of the Class structure of which they are an instance eg. Class Object(s) Pete Collingwood

  8. Job public static int count • SomeOtherClass • someMethod{ • Job job1 = new Job(); • Job.count++; • Job job2 = new Job(); • job2.count++; • } Hmm.. What about static? • When objects are set up, only the non-static elements are included in the copied structure. • Objects can still be used to reference static elements, but we are only referencing the one (unique) element sitting in the class. In this sense, static elements are global data – so care should be taken!!! • static elements can also be referenced just via the Classname Pete Collingwood

  9. Objects & Object Oriented Development. • Normally, an OO application is viewed as a set of interacting objects (not classes). • Object interaction means message passing and method invocation: message passing: one object (the “client”) sends a message to another object (the “server”) asking for some service to be performed. method invocation: when the server object receives a message, it responds by executing (invoking) the appropriate method. • NOTE: the same (or different) client object can send the same message to different server objects, but the response (consequences) might be different depending on the method(s) invoked. Pete Collingwood

  10. Identifying what classes Identifying what objects organisation Structure & Contentof classes. Identifying which Objects interact & how Scenario’s Fitting it all together message passing method invocation attributes methods The Challenge of OO Development • How to find the (right) objects…. • CLUE: use a sort of "recipe" approach – • identify self-contained elements of system functionality • describe the sequence of events in that unit. pressButton //in mainInterface • launch application • create system objects • display main Interface Pete Collingwood

  11. Objects in Java • Declaring objects (The easy bit) Already seen examples of this syntax: <Class name> <objectname>; • Use the Constructor (the new bit) • before we can use an object, it must be initialised. • to initialise an object, we invoke its class constructor…… • Use the key word new Pete Collingwood

  12. Constructors • methods are also referred to as either: • accessors • mutators, or • destructors • Constructors are special public methods defined in a class. • they have the same name as the class • there can be more than one constructor, each having a different set of parameters. • Constructors do not have return values. • In Java, there is a default parameterless constructor which just sets up the relevant structures. This can be overwritten by defining your own version. • Typically, the constructor is responsible for initialising an objects attributes, but can also be used to execute other methods. Pete Collingwood

  13. Examples of Constructors. public class Message{ static count = 0; String to_field; String from_field; public Message(String to, String from ){ to_field = to; from_field = from; count = count + 1;} //etc } • public class EmailClient{ • /* attributes */ • public EmailClient( ){ • /* do some stuff */ • displayGUI( ); • } • //etc • } Pete Collingwood

  14. Objects in Java • Declaring objects (The easy bit) Already seen examples of this syntax: <Class name> <objectname>; • Use the Constructor (the new bit) • before we can use an object, it must be initialised. • to initialise an object, we invoke its class constructor…… • Use the key word new: <Classname> <objectname> = newclassConstructor(/* plus parameters*/); Example Message my_message = new Message("Bill", "Fred"); Pete Collingwood

  15. Invoking an Objects methods. • Now we know how to set up objects (technically), how do we use them? • Essentially, this boils down to message passing and method invocation. • In Java the "dot" notation is used as we've already seen: serverObject.methodName(/*listofparameters*/); Pete Collingwood

  16. Finding Objects (physically) NOTE: . • this call will be made from within a client object • the clientObject must "know about" the serverObject in order for the reference to make sense. • This can happen in a number of ways, but typically, • the server object is a static attribute of another class: System.in.read(); • there is a local object, of serverObject type declared within the clientObject. This serves as a formal parameter, specifying how the client interacts with the server. An actual "value" of a server object is then passed in as a parameter to the client (eg as a parameter in a Constructor). Pete Collingwood

  17. An aside... Writing Methods • As we've seen, defining classes and getting objects to interact largely involves writing methods. • It is important to remember that methods are just procedures so we can use all of the programming & design skills from PseudoCode, C++, javascript or VB or.. to write them. • The methods are enhanced by the ability to include other objects and objects methods (ie procedures in another Class) within their execution. Pete Collingwood

  18. A Case Study • The attached code is a simple simulation of a printer Queue. • A user creates a printJob and sends it to the Printer. • there the Job is stored until it is its turn to be printed. • To simulate the activity of the printer simple, text-based i/o is used. • Various versions of the application are developed, starting from a fairly "procedural" approach (SimpleSim) to an OO version (Simulator_v4). • Look at each of Sim* classes and identify which OO features are used where (eg. which classes, which objects, which messages, how...) • In the remainder of the tutorial, modify Simulator_v4 so that the "Delete a Print Job" option works. Pete Collingwood

  19. Summary • Introduced Objects as variables of a type specified by a given Classname. • Removed (unnecessary) static's from a Class definition. • Shown how objects interact through message passing/method invocation. • Shown how to create objects. • Studied a simple Case Study to identify the typical structure of an OO application. Pete Collingwood

More Related