Lesson 3.3: Object-Oriented Programming Flashcards
In Java، what are packages and what are interfaces?
Packages are groups of related classes. Packages help organize your code and provide another layer of encapsulation. As you will see in Chapter 15، packages also play an important role with modules. An interface defines a set of methods that will be implemented by a class. Thus، an interface gives you a way to specify what a class will do، but not how it will do it.
What are the two main purposes of packages?
First، it provides a mechanism by which related pieces of a program can be organized as a unit. Classes defined within a package must be accessed through their package name. Thus، a package provides a way to name a collection of classes. Second، a package participates in Java’s access control mechanism. Classes defined within a package can be made private to that package and not accessible by code outside the package. Thus، the package provides a means by which classes can be encapsulated
What are namespaces in Java?
In general، when you name a class، you are allocating a name from the namespace. A namespace defines a declarative region. In Java، no two classes can use the same name from the same namespace. Thus، within a given namespace، each class name must be unique.
What happens to a class if you don’t assign it a package?
All classes in Java belong to some package. When no package statement is specified، the default package is used. Furthermore، the default package has no name، which makes the default package transparent.
How do you create a package and what is its general form?
To create a package، put a package command at the top of a Java source file. The classes declared within that file will then belong to the specified package. Since a package defines a namespace، the names of the classes that you put into the file become part of that package’s namespace. This is the general form of the package statement: package pkg; Here، pkg is the name of the package.
If you define a class as part of a package، can you use other packages in that class?
The package statement simply specifies to which package the classes defined in a file belong. It does not exclude other classes in other files from being part of that same package. Most real-world packages are spread across many files.
How do you create packages inside other packages?
You can create a hierarchy of packages. To do so، simply separate each package name from the one above it by use of a period. The general form of a multileveled package statement is shown here: package pack1.pack2.pack3…packN; Page 276Of course، you must create directories that support the package hierarchy that you create.
How does the Java run-time system know where to look for packages that you create?
the answer has three parts. First، by default، the Java run-time system uses the current working directory as its starting point. Thus، if your package is in a subdirectory of the current directory، it will be found. Second، you can specify a directory path or paths by setting the CLASSPATH environmental variable. Third، you can use the -classpath option with java and javac to specify the path to your classes.
Where should you keep a java and class file? Where should it be compiled from?
To avoid problems، it is best to keep all .java and .class files associated with a package in that package’s directory. Also، compile each file from the directory above the package directory.
What happens when a member of a class with package membership doesn’t have an explicit modifier?
If a member of a class has no explicit access modifier، then it is visible within its package but not outside its package. Therefore، you will use the default access specification for elements that you want to keep private to a package but public within that package.
What does it mean to declare a member of a class with membership in a package when declared public or private?
Members explicitly declared public are the most visible، and can be accessed from different classes and different packages. A private member is accessible only to the other members of its class.
When a member of a package is declared as protected، what is its accessibility?
A member specified as protected is accessible within its package and to subclasses in other packages.
What type of members have access to the protected modifier?
the protected modifier creates a member that is accessible within its package and to subclasses in other packages. Thus، a protected member is available for all subclasses to use but is still protected from arbitrary access by code outside its package.
What does the import statement mean and what is it’s general form?
Using import you can bring one or more members of a package into view. This allows you to use those members directly، without explicit package qualification. Page 283Here is the general form of the import statement: import pkg.classname; Here، pkg is the name of the package، which can include its full path، and classname is the name of the class being imported. If you want to import the entire contents of a package، use an asterisk (*) for the class name.
Where do you put import statement in a code?
In a Java source file، import statements occur immediately following the package statement (if it exists) and before any class definitions.
What is the Java API?
Java defines a large number of standard classes that are available to all programs. This class library is often referred to as the Java API (Application Programming Interface). The Java API is stored in packages. At the top of the package hierarchy is java. Descending from java are several subpackages.
Why is the java.lang subpackage unique?
java.lang. It contains، among several others، the System class، which you have been using when performing output using println( ). The java.lang package is unique because it is imported automatically into every Java program.
What is an interface in Java?
An interface is syntactically similar to an abstract class، in that you can specify one or more methods that have no body. Those methods must be implemented by a class in order for their actions to be defined. Thus، an interface specifies what must be done، but not how to do it. Once an interface is defined، any number of classes can implement it. Also، one class can implement any number of interfaces.
What is the general form of a traditional interface?
Here is a simplified general form of a traditional interface: access interface name { ret-type method-name1(param-list); ret-type method-name2(param-list); type var1 = value; type var2 = value; // … ret-type method-nameN(param-list); type varN = value; } For a top-level interface، access is either public or not used. When no access modifier is included، then default access results، and the interface is available only to other members of its package. When it is declared as public، the interface can be used by any other code. (When an interface is declared public، it must be in a file of the same name.) name is the name of the interface and can be any valid identifier، except for var (which is a reserved type name added by JDK 10).
What is the form of a class that includes the implements clause? What if you want to implement more that one interface?
The general form of a class that includes the implements clause looks like this: class classname extends superclass implements interface { // class-body } Page 286To implement more than one interface، the interfaces are separated with a comma. Of course، the extends clause is optional.
When is a class considered “abstract”? Why are they benificial?
If a class includes an interface but does not fully implement the methods defined by that interface، then that class must be declared abstract. No objects of such a class can be created، but it can be used as an abstract superclass، allowing subclasses to provide the complete implementation.
***TT 8-1
“1. Create a file called ICharQ.java and put into that file the following interface definition: // A character queue interface. public interface ICharQ { //Put a character into the queue. void put(char ch); //Get a character from the queue. char get(); } As you can see، this interface is very simple، consisting of only two methods. Each class that implements ICharQ will need to implement these methods. 2. Create a file called IQDemo.java. 3. Begin creating IQDemo.java by adding the FixedQueue class shown here: //A fixed-size queue class for characters. class FixedQueue implements ICharQ { private char q[]; //This array holds the queue. private int putloc، getloc; //The put and get indices //Construct an empty queue given its size. public FixedQueue(int size) { q = new char[size]; //Allocate memory for queue. putloc = getloc = 0; } //Put a character into the queue. public void put(char ch) { if(putloc == q.length) { System.out.println(“ Queue is full.”); return; } q[putloc++] = ch; } //Get a character from the queue. public char get() { if(getloc == putloc) { System.out.println(“ Queue is empty.”); return (char) 0; } return q[getloc++]; } } This implementation of ICharQ is adapted from the Queue class shown in Chapter 5 and should already be familiar to you. 4. To IQDemo.java add the CircularQueue class shown here. It implements a circular queue for characters. //A circular queue. class CircularQueue implements ICharQ { private char q[]; //This array holds the queue. private int putloc، getloc; //The put and get indices //Construct an empty queue given its size. public CircularQueue(int size) { q = new char[size + 1]; //Allocate memory for queue. putloc = getloc = 0; } //Put a character into the queue. public void put(char ch) { /Queue is full if either putloc is one less than getloc or if putloc is at the end of the array and getloc is at the beginning./ if(putloc + 1 == getloc | ((putloc == q.length 1) & (getloc == 0))) { System.out.println(“ Queue is full.”); return; } q[putloc++] = ch; if(putloc == q.length) putloc = 0; //Loop back. } //Get a character from the queue. public char get() { if(getloc == putloc) { System.out.println(“ Queue is empty.”); return (char) 0; } char ch = q[getloc++]; if(getloc == q.length) getloc = 0; //Loop back. return ch; } } The circular queue works by reusing space in the array that is freed when elements are retrieved. Thus، it can store an unlimited number of elements as long as elements are also being removed. While conceptually simple—just reset the appropriate index to zero when the end of the array is reached—the boundary conditions are a bit confusing at first. In a circular queue، the queue is full not when the end of the underlying array is reached، but rather when storing an item would cause an unretrieved item to be overwritten. Thus، put( ) must check several conditions in order to determine if the queue is full. As the comments suggest، the queue is full when either putloc is one less than getloc، or if putloc is at the end of the array and getloc is at the beginning. As before، the queue is empty when getloc and putloc are equal. To make these checks easier، the underlying array is created one size larger than the queue size. 5. Put into IQDemo.java the DynQueue class shown next. It implements a “growable” queue that expands its size when space is exhausted. // A dynamic queue. class DynQueue implements ICharQ { private char q[]; // This array holds the queue private int putloc، getloc; // The put and get indices // Construct an empty queue given its size. public DynQueue(int size) { q = new char[size]; // Allocate memory for queue putloc = getloc = 0; } // Put a character into the queue. public void put(char ch) { if (putloc==q.length) { // Increase queue size char t[] = new char[q.length * 2]; // Copy elements into new queue for(int i=0; i < q.length; i++){ t[i] = q[i]; } q = t; } q[putloc++] = ch; } // Get a character from the queue. public char get(){ if(getloc == putloc){ System.out.println(“ Queue is empty.”); } return (char) 0; } } In this queue implementation، when the queue is full، an attempt to store another element causes a new underlying array to be allocated that is twice as large as the original، the current contents of the queue are copied into this array، and a reference to the new array is stored in q. 6. To demonstrate the three ICharQ implementations، enter the following class into IQDemo.java. It uses an ICharQ reference to access all three queues. // Demonstrate the ICharQ interface. class IQDemo { public static void main(String args[]) { FixedQueue q1 = new FixedQueue (10); DynQueue q2 = new DynQueue (5); CircularQueue q3 = new CircularQueue (10); ICharQ iQ: char ch; int i; iQ = q1; // Put some characters into fixed queue. for (i=0; i < 10; i++){ iQ.put((char) (‘A’ + i)); } // Show the queue. System.out.println(“Contents of fixed queue: “); for(i=0; i<10; i++) { ch = iQ.get(); System.out.print(ch); } System.out.println(); iQ = q2; // Put some characters into dynamic queue. for(i=0; i<10; i++){ iQ.put((char) (‘Z’ 1)); // Show the queue. System.out.print(“Contents of dynamic queue: “); for(i=0; i<10; i++) { ch iQ.get(); System.out.print(ch); } System.out.println(); iQ = q3; // Put some characters into circular queue. for(i=0; i<10; i++) { iQ.put((char) (‘A’ + i)); // Show the queue. System.out.print(“Contents of circular queue: “); for(i=0; i<10; i++) { ch iQ.get(); System.out.print (ch); } System.out.println(); // Put more characters into circular queue. for(i=10; i < 20; 1++) { iQ.put((char) (‘A’ + i)); } // Show the queue . System.out.print(“Contents of circular queue: “); for(i=0; i < 10; i++) { ch = iQ.get(); System.out.print (ch); } System.out.println(“\nStore and consume from” + “ circular queue.”); // Store in and consume from circular queue. for(i=0; i< 20; i++) { iQ.put((char) (‘A’ + i)); ch = iQ.get(); System.out.print (ch); } } } 7. The output from this program is shown here: Contents of fixed queue: ABCDEFGHIJ Contents of dynamic queue: ZYXWVUTSRQ Contents of circular queue: ABCDEFGHIJ Contents of circular queue: KLMNOPQRST Store and consume from circular queue. ABCDEFGHIJKLMNOPQRST 8. Here are some things to try on your own. Create a circular version of DynQueue. Add a reset( ) method to ICharQ، which resets the queue. Create a static method that copies the contents of one type of queue into another.