Skip to main contentCal State San Bernardino / [CNS] / [Comp Sci Dept] / [R J Botting] / [CSci320] [Search ]
[Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Grading] [Contact]
Sessions: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
15.html (HTML) [15.txt(Text)] Tue May 29 19:35:51 PDT 2007

Contents


    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 8lab16 Java102

      Preparation

      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 3 or more review questions.

      Notes

        There are No abstract objects -- just abstract classes

      1. All objects are constructed as an instance of a concrete class.
      2. You can have variables that point at abstraction.....
      3. but can not construct abstract objects.
      4. An animal is an abstraction... but each cat and dog is a concrete example of an animal.

        Abstract vs Concrete

        Try out this program: [ 15.cpp ]

        Objects know how to do things -- Polymorphism

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

        An object behaves the same however we refer to it.

      7. In a non-object-oriented language the behavior depends on its name.
      8. 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 ]

      Next

      [ 16.html ]

    End