- In Java, classes can have
- multiple parents
- one and only one parent
- at most two parents
This is called single inheritance.
Java classes can implement any number of interfaces.
- Private methods in the parent class are
- public in the child class
- private in the child class
- protected in the child class
Protected methods are public in the child class.
- Given the classes defined below
public class parent {
public void answer() { System.out.println("99"); }
public void guess() {System.out.println("102");}
}
public class child1 extends parent
{
public void answer() {System.out.println("42");}
public void guess() {System.out.println("84");}
}
public class child2 extends parent
{
public void guess() {System.out.println("101");}
}
and some instances
parent p = new parent();
child1 c1 = new child1();
child2 c2 = new child2();
For each method call below, what is printed?
- c1.answer(); 42
- c2.answer(); 99
- c2.guess(); 101
The child2 class doesn't have an answer() method, so it used the one
it inherited from its parent.
- Using the classes described above, is it legal to do this?
parent p2 = new child2();
- Yes
- No
Variables of the parent type can hold objects of any child
type, grandchild type and so on.
This is the nature of the is-a inheritance relationship.
- If a class implements an interface, it
must have methods that override all the
ones in the interface
- True
- False
This is different from inheritance where you can override
methods from the parent class, but don't have to.
- If an exception occurs in a try block
the catch statement is executed
- after executing the rest of the statements in the try block
- before exceuting the rest of the statments in the try block
- immediately, the rest of the statements
in the try block are not executed
Control jumps to the catch block that matches the exception
in the nearest surrounding try block.
- In this bit of code, what is printed
// some code
try {
// some code
try {
x.method(); // method throws MyException
} catch(Myexception m2) {
System.out.println("inner try");
}
} catch MyException m1) {
System.out.println("outer try");
}
- outer try
- inner try
The outer catch would only be done if the inner one
threw the exception again.
- Java models input and output as
- streams
- ponds
- a network of pipes
- If you create a File object like this:
File f = new File("testdata.txt");
Which method calls are legal?
- f.exists()
- f.read()
- f.println()
The File class represents the file, so you can only use it to check on the
file.
Other classes represent reading and writing the file.
- Which of the following is true for JPanels?
- They can hold exactly one component
- They can hold many components but no panels
- They can hold components or panels
Panels are mainly used as containers to organize the
position and appearance of other graphical components.
- Which of the following is true for listeners?
- They can only be an inner class
- They can be any class that implements the listener interface
- Are created when the graphic component is created
We saw several ways to make listeners, inner classes were only
one of them.
- Threads can only be used to handle graphics
- True
- False
They can be used to do anything.
Other examples we discussed were to do database or network
I/O while computation was being done.