Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #13975

Re: beginning java...nublet drowning. throw me a lifeline?

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: beginning java...nublet drowning. throw me a lifeline?
Date 2012-04-28 12:42 -0700
Organization albasani.net
Message-ID <jnhh7f$b90$1@news.albasani.net> (permalink)
References <89baf7bb-7b1d-4bc0-80ba-a4881de09810@t16g2000yqt.googlegroups.com> <jnfo4f$7fu$1@dont-email.me>

Show all headers | View raw


markspace wrote:
> Justin Fondriest wrote:
>> Ok...so is this saying "a car has an engine", with regard to code
>> would be more like "a car (class)"--as in every car class--"has an
>> engine(object)"?
>
> This is more or less correct. Don't forget that the engine is also a class,
> and not an object, until you instantiate the Car, then you have both a Car
> object and an Engine object;
>
> class Car {
> Engine engine = new Engine();
> }
>
> There's no objects in the above code until someone calls "new Car()".
>
>>
>>
>> Now whether I am right or wrong...could someone take maybe 3 minutes
>> to explain what composition is in layman's terms?
>
> You were pretty close.
>
>>
>> does it have anything to do with the "import" feature that you put in
>> a program before you "instantiate"(?) the class?
>
> Nope, not a darn thing.

You should google around for these concepts, too. Perhaps the most essential 
skill in programming is the ability to find out stuff when there's no one 
around to ask. What research did you do before asking here?

The Java tutorials discuss 'import'. 'import' has no functional action in a 
program. It is simply a declaration to the compiler which of several possible 
packages you mean to use for a given name.

Composition - "has-a" - the meaning of "has-a" is intended to be as in English 
- something has something.

Inheritance or Extension (in the Java sense) - "is-a" - the meaning of "is-a" 
is intended to be as in English - something is something.

The Car analogy is very simplified, remember, but it makes the case.

Is an engine a car?
Is a car an engine?

No. "is-a" doesn't apply, so we do not see 'public class Car extends Engine'.

In our model (but not necessarily in real life), a car "has an" engine, so we 
use the "has-a" cue, meaning the class 'Car' has (yep, has) an element to 
represent the engine.

  public class Car
  {
    private Engine engine; // + associated get...(), set...() methods
  }

Putting a member inside a class definition is called "composition" because a 
class's shape, or structure, is composed of those members.

For most purposes, composition is better than inheritance in Java.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

beginning java...nublet drowning.  throw me a lifeline? Justin Fondriest <jfondriest82@gmail.com> - 2012-04-27 16:20 -0700
  Re: beginning java...nublet drowning.  throw me a lifeline? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-27 21:48 -0400
  Re: beginning java...nublet drowning.  throw me a lifeline? markspace <-@.> - 2012-04-27 20:28 -0700
    Re: beginning java...nublet drowning.  throw me a lifeline? Lew <noone@lewscanon.com> - 2012-04-28 12:42 -0700
  Re: beginning java...nublet drowning.  throw me a lifeline? Roedy Green <see_website@mindprod.com.invalid> - 2012-04-27 20:48 -0700
    Re: beginning java...nublet drowning.  throw me a lifeline? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-04-28 11:56 -0300

csiph-web