Answer the following questions as briefly (but completely) as possible:
C
(via the command
“java C
”) in the code below (saved
in C.java
)?
(And, is that what you expected?)
class A { public A () { System.out.println( "A's no-arg constructor is invoked" ); } } class B extends A { } public class C { public static void main ( String [] args ) { B b = new B(); } }
class A { public A ( int x ) { } } class B extends A { public B () { } } public class C { public static void main ( String [] args ) { B b = new B(); } }
@Override
annotation?
public class Test { public static void main ( String [] args ) { A a = new A( 3 ); } } class A extends B { public A ( int t ) { System.out.println( "A's constructor is invoked" ); } } class B { public B () { System.out.println( "B's constructor is invoked" ); } }
Object
invoked when
new A(3)
is invoked?
public class Test { public static void main ( String [] args ) { Object fruit = new Fruit(); Object apple = (Apple) fruit; } } class Apple extends Fruit { } class Fruit { }
equals
method, a common mistake is
mistyping its signature in the subclass.
For example, the equals
method is incorrectly written as
equals(Circle circle)
as shown in (a) below.
It should be written as equals(Object circle)
,
as shown in (b) below.
Show the output of running class Test
using the Circle
class first from (a), and then from (b).
Explain the output.
public class Test { public static void main ( String [] args ) { Object circle1 = new Circle(); Object circle2 = new Circle(); System.out.println( circle1.equals( circle2 ) ); } }
class Circle { double radius; public boolean equals ( Circle circle ) { return this.radius == circle.radius; } }
class Circle { double radius; public boolean equals ( Object circle ) { return this.radius == ((Circle) circle).radius; } }
Next, try adding the “@Override
” annotation to the
equals
method in (a), and then try compiling.
Repeat with (b).
What are the results, and are they as you expected?
class A { abstract void unfinished ( ) { } }
public class abstract A { abstract void unfinished ( ) { } }
class A { abstract void unfinished ( ) ; }
abstract class A { protected void unfinished ( ) ; }
abstract class A { abstract void unfinished ( ) ; }
class A { abstract int unfinished ( ) ; }
A
is an interface.
Can you create an instance using
“new A()
”?
I1
and I2
are correctly defined
elsewhere.)
interface A { void print () { }; }
abstract interface A extends I1, I2 { abstract void print () { }; }
abstract interface A { print () ; }
interface A { void print () ; }
Email your homework assignment, by copy-and-paste (no attachments please), to (homework submission). If possible use the “text” and not the “HTML” mode of your email program. Please use the subject similar to “Java Programming I Homework Assignment #5 Submission”, so I can tell which emails are submitted homework assignments.
Homework assignments will not be returned. Please do not send as attachments. Refer to the Homework and the Submitting Assignments sections of your syllabus for more information.
Confused? Send questions about the homework assignment to (homework questions). Please use a subject similar to “Java Programming I Homework Assignment #5 Questions” so I can tell which emails are questions about assignment quiz (and not submissions).