Java Reference
In-Depth Information
13.22
✓
✓
Can you invoke the
clone()
method to clone an object if the class for the object
does not implement the
java.lang.Cloneable
? Does the
Date
class implement
Cloneable
?
Check
Point
13.23
What would happen if the
House
class (defined in Listing 13.11) did not override the
clone()
method or if
House
did not implement
java.lang.Cloneable
?
13.24
Show the output of the following code:
java.util.Date date =
new
java.util.Date();
java.util.Date date1 = date;
java.util.Date date2 = (java.util.Date)(date.clone());
System.out.println(date == date1);
System.out.println(date == date2);
System.out.println(date.equals(date2));
13.25
Show the output of the following code:
ArrayList<String> list =
new
ArrayList<>();
list.add(
"New York"
);
ArrayList<String> list1 = list;
ArrayList<String> list2 = (ArrayList<String>)(list.clone());
list.add(
"Atlanta"
);
System.out.println(list == list1);
System.out.println(list == list2);
System.out.println(
"list is "
+ list);
System.out.println(
"list1 is "
+ list1);
System.out.println(
"list2.get(0) is "
+ list2.get(
0
));
System.out.println(
"list2.size() is "
+ list2.size());
13.26
What is wrong in the following code?
public class
Test {
public static void
main(String[] args) {
GeometricObject x =
new
Circle(
3
);
GeometricObject y = x.clone();
System.out.println(x == y);
}
}
A class can implement multiple interfaces, but it can only extend one superclass.
Key
Point
An interface can be used more or less the same way as an abstract class, but defining
an interface is different from defining an abstract class. Table 13.2 summarizes the
differences.
T
ABLE
13.2
Interfaces vs. Abstract Classes
Variables
Constructors
Methods
Abstract class
No restrictions.
Constructors are invoked by subclasses through
constructor chaining. An abstract class cannot be
instantiated using the new operator.
No restrictions.
Interface
All variables must be
public static final
.
No constructors. An interface cannot be instantiated
using the new operator.
All methods must be public
abstract instance methods
Search WWH ::
Custom Search