|
Ok, so just to make sure I understand. Animal animal = new Cat(); Methods fron the class Animal are available to animal. Unique methods in the Cat class are not available to animal. If a method exists in both Animal and Cat, the methods in Cat will be used. Animal contains methods isMammal, hasFur, isPoisonous Cat contains methods hasFur, breed I can do animal.isMamal(), aninmal.hasFur(), and animal.isPoisonous() I cannot do animal.breed() If I used animal.hasFur(), the method from the Cat class will be used, not the method from the Animal class. Is this right? Brad > -----Original Message----- > From: Joe Pluta [mailto:joepluta@plutabrothers.com] > Sent: Wednesday, February 07, 2001 10:28 AM > To: JAVA400-L@midrange.com > Subject: RE: Instantiation question > > > In Java, every variable is identified by its type and its > name. Non-primitive variables are in effect pointers to > objects. I call them handles. The type tells the compiler > what characteristics to apply to the object that will > eventually reside in the handle. > > Why is this important? Because you can do the following > (provided Dog is a subclass of Animal): > > Animal animal = new Dog(); > > At this point, you can only call the methods associated with > Animal, not those unique to Dog. This technique is used when > you want to deal with objects of different types at a higher > level of abstraction. Also, this allows the use of > polymorphism by causing different methods to be called based > on the actual type of the object stored in the handle. This > is because, if a class overrides a method of its superclass, > when the method is called, the overriding method is called > regardless of the type of the handle. (Whew!) > > Specifically, let's say that class Animal has a method called > "isMammal()" that returns a boolean value of false. In this > case, if Dog overrides isMammal and returns true, then > "animal.isMammal()" will return true, not false. This is the > most powerful aspect of polymorphism. > > Another side effect (specific to Java's singly-rooted object > hierarchy) is that, carried to its extreme, any object can be > stored in a handle of type Object. This allows, for example, > heterogenous sets like Vectors to contain any type of object. > > > ---------- Original Message ---------------------------------- > From: "Stone, Brad V (TC)" <bvstone@taylorcorp.com> > Reply-To: JAVA400-L@midrange.com > Date: Wed, 7 Feb 2001 09:57:00 -0600 > > >I'll let others answer you question, but I'd like to expand on your > question. > > Why do you need to say: > > Student show = new Student(); > > Why not just: > > show = new Student(); > > Is there a case where you would want to do something like: > > Student show = new Principal(); > or > Principal show = new Student(); > > Maybe the answer to your original question will answer mine. :) I've > always thought that this type of declaration was a bit redundant. > > Brad > > +--- > | This is the JAVA/400 Mailing List! > | To submit a new message, send your mail to JAVA400-L@midrange.com. > | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. > | To unsubscribe from this list send email to > JAVA400-L-UNSUB@midrange.com. > | Questions should be directed to the list owner: joe@zappie.net > +--- > +--- | This is the JAVA/400 Mailing List! | To submit a new message, send your mail to JAVA400-L@midrange.com. | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. | To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com. | Questions should be directed to the list owner: joe@zappie.net +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.