OCJP

49 views
Skip to first unread message

Murad İmanbəyli

unread,
Dec 16, 2011, 2:53:44 PM12/16/11
to IndiJava - India Java User Group & Java Community.
why answer A? Thank you in advance

3. class Mammal {
4. String name = "furry ";
5. String makeNoise() { return "generic noise"; }
6. }
7. class Zebra extends Mammal {
8. String name = "stripes ";
9. String makeNoise() { return "bray"; }
10. }
11. public class ZooKeeper {
12. public static void main(String[] args) { new ZooKeeper().go(); }
13. void go() {
14. Mammal m = new Zebra();
15. System.out.println(m.name + m.makeNoise());
16. }
17. }

What is the result?
A. furry bray
B. stripes bray
C. furry generic noise
D. stripes generic noise
E. Compilation fails
F. An exception is thrown at runtime

Vishal Kukreja

unread,
Dec 17, 2011, 1:24:39 AM12/17/11
to indi...@googlegroups.com
In  ZooKeeper class:
void go()
{
Mammal m = new Zebra();
System.out.println(m.name + m.makeNoise());
} 
Here Reference Mammal is given, but polymorphically(with reference of Mammal)  it refers to a Mammal Subclass instance (Zebra in this case). So at run-time object(zebra) will run its own makeNoise() method. So result of m.makeNoise():bray
(This case is applied to Methods at runtime)

About m.name: it's calling 'name' of superclass only.
Java Guru's, correct me if I am wrong.

2011/12/17 Murad İmanbəyli <muradim...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "IndiJava - India Java User Group & Java Community." group.
To post to this group, send email to indi...@googlegroups.com
To unsubscribe from this group, send email to indijava-u...@googlegroups.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.indijava.in -  India Java User Group & Java Community.
http://groups.google.com/group/indijava/

Java Fresher Jobs : http://www.indijava.in/community/fresher-jobs
Quiz : http://www.indijava.in/community/QUIZ
Java News: http://www.indijava.in/community/java-news
Tutorials: http://www.indijava.in/community/tutorials
Forum http://www.indijava.in/community/forum
SCJP Resources : http://www.indijava.in/community/category/certifications/scjp

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



--

Regards,
Vishal Kukreja

Ashish Agre

unread,
Dec 17, 2011, 5:39:08 AM12/17/11
to indi...@googlegroups.com
ploymorphics behavior is not applicable to instance variable.

Murad İmanbəyli

unread,
Dec 17, 2011, 2:09:36 AM12/17/11
to IndiJava - India Java User Group & Java Community.
thanks,Vishal Kukreja

On Dec 17, 10:24 am, Vishal Kukreja <vishalkukrej...@gmail.com> wrote:
> In  ZooKeeper class:
> void go()
> {
>
> Mammal m = new Zebra();
>
> System.out.println(m.name + m.makeNoise());
>
> }
>
> Here Reference Mammal is given, but polymorphically(with reference of
> Mammal)  it refers to a Mammal Subclass instance (Zebra in this case). So
> at run-time object(zebra) will run its own makeNoise() method. So result

> of m.makeNoise():*bray*


> (This case is applied to Methods at runtime)
>
> About m.name: it's calling 'name' of superclass only.
> Java Guru's, correct me if I am wrong.
>

> 2011/12/17 Murad İmanbəyli <muradimanba...@gmail.com>

> >http://www.indijava.in-  India Java User Group & Java Community.

> > Forumhttp://www.indijava.in/community/forum


> > SCJP Resources :
> >http://www.indijava.in/community/category/certifications/scjp
>
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> --
>
> Regards,

> <http://goog_968477932>Vishal Kukreja

Murad İmanbəyli

unread,
Dec 17, 2011, 10:55:25 AM12/17/11
to IndiJava - India Java User Group & Java Community.
Thanks

On 17 Aralık, 10:24, Vishal Kukreja <vishalkukrej...@gmail.com> wrote:
> In  ZooKeeper class:
> void go()
> {
>
> Mammal m = new Zebra();
>
> System.out.println(m.name + m.makeNoise());
>
> }
>
> Here Reference Mammal is given, but polymorphically(with reference of
> Mammal)  it refers to a Mammal Subclass instance (Zebra in this case). So
> at run-time object(zebra) will run its own makeNoise() method. So result

> of m.makeNoise():*bray*


> (This case is applied to Methods at runtime)
>
> About m.name: it's calling 'name' of superclass only.
> Java Guru's, correct me if I am wrong.
>

> 2011/12/17 Murad İmanbəyli <muradimanba...@gmail.com>

> >http://www.indijava.in-  India Java User Group & Java Community.

> > Forumhttp://www.indijava.in/community/forum


> > SCJP Resources :
> >http://www.indijava.in/community/category/certifications/scjp
>
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> --
>
> Regards,

> <http://goog_968477932>Vishal Kukreja

Murad İmanbəyli

unread,
Dec 17, 2011, 3:07:21 AM12/17/11
to IndiJava - India Java User Group & Java Community.
Thanks,

On Dec 17, 10:24 am, Vishal Kukreja <vishalkukrej...@gmail.com> wrote:

> In  ZooKeeper class:
> void go()
> {
>
> Mammal m = new Zebra();
>
> System.out.println(m.name + m.makeNoise());
>
> }
>
> Here Reference Mammal is given, but polymorphically(with reference of
> Mammal)  it refers to a Mammal Subclass instance (Zebra in this case). So
> at run-time object(zebra) will run its own makeNoise() method. So result

> of m.makeNoise():*bray*


> (This case is applied to Methods at runtime)
>
> About m.name: it's calling 'name' of superclass only.
> Java Guru's, correct me if I am wrong.
>

> 2011/12/17 Murad İmanbəyli <muradimanba...@gmail.com>

> >http://www.indijava.in-  India Java User Group & Java Community.

> > Forumhttp://www.indijava.in/community/forum


> > SCJP Resources :
> >http://www.indijava.in/community/category/certifications/scjp
>
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> --
>
> Regards,

> <http://goog_968477932>Vishal Kukreja

Reply all
Reply to author
Forward
0 new messages