Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #549
| From | Lew <noone@lewscanon.com> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Class ... implements ... |
| Date | 2011-04-06 12:08 -0400 |
| Organization | albasani.net |
| Message-ID | <ini35r$30t$3@news.albasani.net> (permalink) |
| References | <87vcywpg2h.fsf@merciadriluca-station.MERCIADRILUCA> <in9q0h$12p$1@dont-email.me> <87zko7e4he.fsf@merciadriluca-station.MERCIADRILUCA> |
On 04/03/2011 11:31 AM, Merciadri Luca wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Eric Sosman<esosman@ieee-dot-org.invalid> writes:
>
>> 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 predict you will get compile-time
>> errors for all five of them.
>
> Okay, I just tested and received the results I wrote before.
>
> ==
> $ javac Test.java
> Test.java:25: incompatible types
> found : C1
> required: C2
> C2 obj_2 = new C1();
> ^
> Test.java:27: I1 is abstract; cannot be instantiated
> I1 obj_4 = new I1();
> ^
> Test.java:28: I1 is abstract; cannot be instantiated
> C2 obj_5 = new I1();
> ^
> 3 errors
> ==
> so that the three operations I mentioned as invalid are effectively
> (at least to my little knowledge) invalid.
>
> Do you have an explanation about your way of reasoning? My compiler
> might be too old, or something like that.
This question was answered in your first answer:
> Which expressions put a subtype instance into a supertype variable?
>
> Which expressions put a supertype instance into a subtype variable?
>
> How can a subtype variable be sure that a supertype instance is of the correct type?
>
> Are you allowed in Java to instantiate an interface?
>
> Please answer as best you can. You should use the Java tutorials to help you; don't just randomly guess. We will be expecting your responses.
Did you even engage with those hints? If not, why expect more?
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
Back to comp.lang.java.help | Previous | Next — Previous in thread | Find similar | Unroll thread
Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-02 22:13 +0200
Re: Class ... implements ... Lew <noone@lewscanon.com> - 2011-04-02 17:30 -0400
Re: Class ... implements ... Roedy Green <see_website@mindprod.com.invalid> - 2011-04-02 23:09 -0700
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 12:00 +0200
Re: Class ... implements ... Roedy Green <see_website@mindprod.com.invalid> - 2011-04-02 20:53 -0700
Re: Class ... implements ... Stanimir Stamenkov <s7an10@netscape.net> - 2011-04-03 10:15 +0300
Re: Class ... implements ... Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-03 08:42 -0400
Re: Class ... implements ... Roedy Green <see_website@mindprod.com.invalid> - 2011-04-03 06:45 -0700
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 17:29 +0200
Re: Class ... implements ... Patricia Shanahan <pats@acm.org> - 2011-04-03 09:01 -0700
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 18:10 +0200
Re: Class ... implements ... Patricia Shanahan <pats@acm.org> - 2011-04-03 09:32 -0700
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 18:48 +0200
Re: Class ... implements ... Aéris <aeris@imirhil.fr> - 2011-04-03 18:07 +0200
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 18:22 +0200
Re: Class ... implements ... Aéris <aeris@imirhil.fr> - 2011-04-03 19:02 +0200
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 20:15 +0200
Re: Class ... implements ... Lew <noone@lewscanon.com> - 2011-04-06 12:01 -0400
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 17:27 +0200
Re: Class ... implements ... Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-03 14:26 -0400
Re: Class ... implements ... Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-04-03 16:14 -0300
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 21:33 +0200
Re: Class ... implements ... Aéris <aeris@imirhil.fr> - 2011-04-03 22:13 +0200
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 22:24 +0200
Re: Class ... implements ... Lew <noone@lewscanon.com> - 2011-04-06 12:05 -0400
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 17:31 +0200
Re: Class ... implements ... Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-03 14:41 -0400
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 21:45 +0200
Re: Class ... implements ... Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-03 16:40 -0400
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 22:58 +0200
Re: Class ... implements ... Lew <noone@lewscanon.com> - 2011-04-06 12:13 -0400
Re: Class ... implements ... Lew <noone@lewscanon.com> - 2011-04-06 12:10 -0400
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 21:51 +0200
Re: Class ... implements ... Patricia Shanahan <pats@acm.org> - 2011-04-03 13:02 -0700
Re: Class ... implements ... Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 22:23 +0200
Re: Class ... implements ... Lew <noone@lewscanon.com> - 2011-04-06 12:15 -0400
Re: Class ... implements ... Lew <noone@lewscanon.com> - 2011-04-06 12:08 -0400
csiph-web