Select this to skip to main content [CSUSB] >> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CSci620] >> 11 [Source]
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13]
Wed May 5 07:13:40 PDT 2004

Contents


    CS620 Session 11 OO and Java101

      Previous

      [ 10.html ]

      Preparation

      Study the Java Handout and pages 135-143 of the text book. Prepare questions and bring to class. Prepare Notes.

      Topics

        Introduction to Object Oriented Languages

        Reuse

        Evolution of Languages

        Changes in OO Languages

        Inheritance

        Polymorphism

        Java

      . . . . . . . . . ( end of section Topics) <<Contents | Index>>

      Questions

        Explain Parnas's Information-hiding principle

        The idea is that we normally do not need or want to understand the details of how something works, as long as we can use it. Examples: driving a car, using a watch, ...

        In particular we hide data structures behind the functions to use them. So we declare attributes/data as private and functions as public.

        What is a modifier in Java?

        A modifier is written in front of a declaration to change some properties. Examples: public allows anyone to use it. private means that only instances of the class can use it. static means that there is one per class that is shared by all instances of the class. final means that something can not be changed, ...

        Is static in Java like static is C and static in C++?

        Static in Java is like one of the uses of static in C++. It is not like the C use of static. A data item is declared as static if there only one. A static function has no object. So if a method ssss is static in class CCCC then it is called CCCC.ssss(....) not x.ssss(...).

        Are Ada Packages like C++ classes?

        Yes, but simpler. A package does not have to create a new type of data in Ada. A class in C++ always produces a new data types.

        Explain Java, Java Applet, and JavaScript

        Java and JavaScript are different languages. JavaScript is embedded in scripts inside pages. Java is object oriented, but JavaScript is object based.

        A Java Applet is an object in the Applet class. Applet classes can be safely transmitted of the Internet to a browser. It has a rectangular box in a web page and paints this whenever requested to.

        On page 138 are any begins missing

        No!

        Explain instantiation and instance.

        A constructor creates a new instance of a class: an object with all the properties declared in the class. This is called instantiation.

        Explain references and instances

        Do not confuse constructing an instance
         		new ClassNAme(parameters)
        with declaring a variable that can refer to a class
         		ClassNAme variable;

        Variables can refer to instances. The behavior of the variable (in Java) is then determined largely by the type of object (instance) to which it refers.

        Can you instantiate an abstract class?

        No.

        If a class Abstract is declared as abstract then this

         		new Abstract(....)
        is illegal, but this
         		Abstract var;
        is OK.

        How can a subclass access a member of its parent class.

        The parent must give permission with modifier protected and then
         		Parent.memberName....
        is OK in the subclass that extends the parent.

        How do you use a class on a remote server?

        Here is the theory. Firstly the class must be public. Second you must know the URL of the directory in which it is stored. Third you use an import statement and type in the directory (or site) URL BACKWARDS:
         		import edu.csusb.csci.public.faculty.dick.*

        In practice we mostly import standard libraries from Sun.

        Are Constructors inherited?

        In any language where a constructor's name is identical to the name of the class it is impossible for them to be inherited.

        However in C++ we can invoke the constructors of our base classes:

         		Class(parameters):BaseClass(arguments){....}
        and in Java (where each class extends one other super-class) we can call the parent super-class constructor:
         		Class(parameters){ super(arguments); ....}
        unlike in C++ we do not name the superclass! The identifier "super" is always used to pass arguments up the hierarchy.

        Why haven't languages changed the basic form of expressions and assignments since FORTRAN?

        LISP tried to change the form of expressions. Most people, however, are happy with the 200 year old mathematical/algebraic infix notation. So it hasn't changed.

        Prolog and LISP both attempted to do with assignments. But ended up with including them any way.

        Any news of Java 1.5?

        Nothing for the last week or so.

        How is memory allocated in this program?

         		Class c;
         		c = new Class(parameters);
        The first line is a declaration and makes sure that there is a space on the runtime stack, called c and capable of holding the address of a Class type object.

        No space is allocated for an object of type Class until the second line is executed. The new creates space on the heap for an object of type Class and then invokes a constructor to initialize it. The address of this new instance of Class is stored in c.

        What does an instance variable do?

        An instance variable is a part of an object. In C++ we call it a data member. In Java it is called a data field.

        Variable hold information. They change when new values are assigned or when a method is applied to them.

        Where is the middle ground between local and global variables?

        A global variable can used anyway in a program. This allows surprising errors because anything can be done to it.

        A local variable can not be accessed accept in the block where it is decalred.... and it does not exist outside that block.

        Instance variables belong to an object and can be accessed anywhere where the object is available in a program... even globally BUT they can only be touched by the methods declared in their class. As a result they are safer than global variables, but more usable than local variables.

        What are side effects on global variable?

        A side-effect occurs when we call a subprogram and a variable that does not belong to that subprogram changes.

        An example was the fibo_count variable in the last lab which increased by one each time fibo was called.

        What is the difference between public and private in Java

        The meaning of public and private are rather like that in C++. Private data and functions can not be accessed by any code that is not part of the class in which they are declared. Similarly, public items in a class can be called/altered/used by statements in other classes.

        Java adds a special meaning to public, private, and protected classes. Public classes are available for use of the Web. So, all Applets must be public classes! Private classes however can be accessed outside their site/directory/package.

        What is the difference between a package and a library?

        There is little difference. All libraries are packages. However a library is usually designed to used by many programs, and a package is used in only one program.

        Which is easiest to program: Java or C++?

        The one that you use most! Familiarity makes any language easier.

        As a rule you can relax more in Java and not worry about garbage collection, stray pointers, missing '*' and '&', etc.

        Also in Java, by definition you have graphics and access to the Web. These are not part of standard C++.

        Give examples of init() and paint()

        These are in the Laboratory today.

        Init is called to initialize applets and paint to display them.

        Explain implements.

        implements is used to declare a class of objects that share a common collection of public functions. The collection of functions is called an interface. An example is the Runnable interface. If a class implements Runnable then it must define a void function called run().

        A class can implement many interfaces. For example, by implementing the MouseListener interface we promise to right functions that can handle mouse events like the object being clicked!

        The word implements sets up a contract between a class and an interface: it guarantees the all the functions are there. The compiler checks that the guarantee is met. As a result Java program tend to be more reliable. One th other hand, you need to know a lot of interfaces to be able to write useful classes:

         		class MyThing extends Applet implements Runnable, MouseListener, HTTPCaller, ....

        How do you test objects for equality?

         		public class Equivalence{
         		  public static void main(String[]args){
         		    Integer n1=new Integer(47);
         		    Integer n2=new Integer(47);
         		    System.out.println(n1==n2);
         		    System.out.println(n1.equal(n2));
         		  }//main
         		}//Equivalence
        Download [ Equivalence.java ] to get the code.

        The output is

         	false
         	true
        The == symbol compares n1 and n2 to see if they point at the same object and they don't because there are TWO new Integer statements and each constructs a different instance of an Integer.

        We say that we compare the references.

        But equals compares the objects them selves.... using rules that are part of the Integer class.... and finds that they are both 47!

        We are comparing object values.

        Notice that an Integer is a box containing the number 47. Imagine this as preparing a lunch box and putting a cheese sandwich in it. Then we make another lunch box and put another cheese sandwich in it. The boxes a different! The contents equivalent. We can have many Integers with 47 in them,.... and each will be different but with the same (internal) value.

      . . . . . . . . . ( end of section Questions) <<Contents | Index>>

      Laboratory Hello, Java!

      The server was down! [ lab11.html ]

      Next

      Study pages 143-158 on Java and prepare questions and notes. [ 12.html ]

    . . . . . . . . . ( end of section CS620 Session 10 Subprograms) <<Contents | Index>>

    Glossary

  1. BNF::="Backus-Naur Form", for syntax and grammar, developed by Backus and Naur.
  2. EBNF::="Extended " BNF.
  3. HTML::= "HyperText Markup Language", used on the WWW.
  4. HTML_page::syntax= "<HTML>" head body.
  5. Java::="An " OO " Language from Sun".
  6. LISP::= "LISt Processing Language".
  7. LRM::="Language Reference Manual".
  8. OO::="Object-Oriented".
  9. Prolog::="Programming in Logic".
  10. TBA::="To Be Announced".
  11. UML::="Unified Modeling Language".
  12. URL::=Universal_Resource_Locator,
  13. Universal_Resource_Locator::syntax= protocol ":" location, where
    Net
    1. protocol::= "http" | "ftp" | "mailto" | ... ,
    2. location::= O( "//" host) O(pathname).

    (End of Net)
  14. WWW::= See http://www.csci.csusb.edu/dick/cs620/, index to web site for this class.
  15. XBNF::="eXtreme" BNF, developed by the teacher from EBNF, designed to ASCII input of syntax, semantics, and other formal specifications.


Formulae and Definitions in Alphabetical Order