Re: Class ... implements ...
On 4/2/2011 4:13 PM, Merciadri Luca wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I'm given the following program snippet:
==
class C1
{
public int n;
}
interface I1
{
int f();
}
class C2 extends C1 implements I1
{
public int f()
{
n = 18;
}
}
==
I need to say if the following expressions are valid or not
(justifications are trivial once I'm sure of the answer):
1. C1 obj_1 = new C2();
Invalid.
2. C2 obj_2 = new C1();
Invalid.
3. I1 obj_3 = new C2();
Invalid.
4. I1 obj_4 = new I1();
Invalid.
5. C2 obj_5 = new I1();
Invalid.
I've found that 1. and 3. are valid, when the others (2., 4. and 5.)
are invalid. Am I right?
No.
Two suggestions, Merciadri. First, while Usenet is a fine
channel for clearing up tricky questions or getting explanations
of confusing points, it is a poor vehicle for wholesale transfer
of large amounts of information. Get a textbook or find a Web
site that devotes itself to teaching Java; these presentations
will organize the material in ways that make sense (if the writers
are good). By posing a flood of random questions on Usenet you
take a random walk through the field of Java, and you will never
know whether you've covered all the ground or covered it adequately.
Second, STOP posting paraphrases of the code you're asking
questions about! Post the actual code, exactly what you gave to
the compiler, not a half-remembered half-accurate half-assed
approximation. Case in point: Take the code you posted for this
very question, run it through the compiler, and see what the compiler
says about your five queries. I predict you will get compile-time
errors for all five of them.
--
Eric Sosman
esosman@ieee-dot-org.invalid