[CSUSB]
>> [CNS]
>> [Comp Sci Dept]
>> [R J Botting]
>> [CSci620]
>>
11
[Source]
. . . . . . . . . ( end of section Topics) <<Contents | Index>>
Questions
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.
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.
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, ....
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
}//EquivalenceDownload [ Equivalence.java ] to get the code.
The output is
false
trueThe == 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