Java Reference
In-Depth Information
15.24
Show the printout 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));
15.25
Show the printout of the following code:
ArrayList<String> list = new ArrayList<String>();
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());
15.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);
}
}
15.8 Interfaces vs. Abstract Classes
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 inter-
face is different from defining an abstract class. Table 15.2 summarizes the differences.
T ABLE 15.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
Java allows only single inheritance for class extension but allows multiple extensions for
interfaces. For example,
single inheritance
multiple inheritance
public class NewClass BaseClass
Interface1, . . ., InterfaceN {
extends
implements
. . .
}
 
 
 
Search WWH ::




Custom Search