[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] /[CS320 Course Materials] /15.html [15.txt(Text)] [Search ]
Thu May 9 17:14:20 PDT 2013
[Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Grading] [Contact]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]


    CS320/15 OOP and Java

      Prev 14Data AbstractionChapter 2 section 14 + Chapter 11lab14 C/C++ ADTs
      **Project Deadline Phase 2Phase 2 due in: changed EBNF and draft UML (10pts)
      15OO Programming and JavaChapter 2 section 17 + Java Handout + Chapter 12lab15 Java101
      Next 16Concurrency and JavaChapter 13 not sect'n 9lab16 Java102

      (Close Table)


      1. Study Chapter 2 section 17 on Java
      2. Study the Java Handout.
      3. Study the Notes below.
      4. Study chapter 12 (not section 12) on OOP
      5. Check out my notes on Java [ java.html ]
      6. Do the review Questions at the end of chapter 12 but don't do any questions on SmallTalk :-(.
      7. Hand in answers to 2 or more review questions.


        Current Lab Examples Trigger Security Warnings -- Ignore

        I have 98 Java files prepared over the years for this class .. and most or them were compiled using an older, less secure Java compiler. So your browser may display a security warning. I think the code is safe for your browser to download and run. You can, therefore, tell it to access them. You can probably tell it to accept any applet from my web site quite safely.

        The problem may be that I have no easy and simple way of signing and applet with a certification from a trusted authority.

        Object oriented designs are about messages

        Objects compute things by sending messages. Every message is like a function call and goes from one object to another. As a result you can draw a diagram of an OO program that shows the sequence of calls to and from some objects.

        Here is an example of part of a program that simulates a DiceGame that has two Dice and throws them, and reads their values to work out a score:

        [DiceGame creates Dice, Plays game by throwing and read their facevalues]

        The code is in [ java/DiceGame.java ] and [ java/Die.java ] , enjoy!

        There are No abstract objects -- just abstract classes

      1. All objects are constructed as an instance of a concrete class.
      2. An animal is an abstraction... but each cat and dog is a concrete example of an animal. You point at a cat and a dog with the same finger... and always be pointing at an animal.
      3. You can have variables that are declared to point at abstraction.....
      4. but can not construct abstract objects.
      5. You can have variables that are declared to point at abstraction.....
      6. and point them at concrete objects derived from the abstraction.

        Abstract vs Concrete

        Try out this program: [ 15.cpp ]

        Objects know how to do things -- Polymorphism

      7. Cats and dogs behave the same even if we change their names.
      8. Call a cat an animal.... and it still behaves like a cat.

        An object behaves the same however we refer to it.

      9. In a non-object-oriented language the behavior depends on its name.
      10. In an object-oriented language the behavior is determined by the object.

        An example

        1. Base class Person and two extended classes Student and Faculty.
        2. Each class defines an output function that displays the name of the Person.
        3. Students also display their GPA.
        4. Faculty display their rank.
                 Person * pointer;
                 Faculty dick;
                 pointer = & dick;
                        pointer->output(); // name and rank
                 Student jo;
                 pointer = &jo;
                        pointer->output(); // name and GPA
                 pointer = new Student(...);
                        pointer->output(); // name and GPA

        Polymorphism in C++

        C++ is a hybrid language. Polymorphism is an option.

        The default is to cast objects. In other words to change the type of an object to a more abstract type. This can result in programs that have bugs.

        If an object is assigned to another object of a more general (base) type, the specific data is stripped away. It behaves according to the general rules.

        This happens with pass-by-value!

        So in C++ always

        • Use pass by reference and pass by constant reference
        • Do not use rather pass by value.
        • Declare member functions to be virtual.
        • Refer to objects via pointers.

        Full Object-Orientation

        Fully object-oriented languages (Java, SmallTalk, etc.) use late (dynamic) binding by default and these errors are avoided. The cost is that the program runs slower.

      . . . . . . . . . ( end of section Notes) <<Contents | End>>

      Class Work

      [ 15q.html ]

      Lab Work

      [ lab/15.html ]


      [ 16.html ]

    ( End of document ) <<Contents | Top