• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding the Object

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following is correct if Bar IS-A Object

1.class Foo { Bar bar;}
2.class Bar { }
3.class Foo extends Bar{ }
4.class Bar extends Foo{ }

Eventhough its simple..i just want to confirm.

in option 1 Bar is reference..
i think option 2 and 4 are correct
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a poorly worded question since all objects will satisfy "IS-A Object". Therefore, you could argue that (1) is OK since Bar implicitly extends Object and (3) is OK since the base class Bar again implicitly extends Object.

Sid
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.class Foo { Bar bar;}

This is incorrect, as we cannot say for sure if Bar is an object type from which concrete instances are made, or if bar in an interface. Regardless, at runtime, an object that is-a bar is required, regardless of whether Bar is an interface, abstract class, or concrete class. If Bar is an interface, at runtime, the class implementing the Bar interface will indeed be an Object.


2.class Bar { }

All classes inherit from java.lang.Object, meaning in this case Bar is-a type of Object.


3.class Foo extends Bar{ }

Classes can only inherit from other classes, so in this case, Bar is either an abstract class, or a concrete class, both of which would have java.lang.Object as an ancestor, so in this case, Bar is-a type of object.


4.class Bar extends Foo{ }

All classes in Java extend java.lang.Object as their most distant ancestor. In this case, Bar is a class, so it is-a type of Object.


So, 1 is pretty iffy, whereas 2,3 and 4 are all positively objects.

You might want to take my OOA & D quiz on www.scja.com There are more questions (and answers) on the subject there.

Cheers!

-Cameron McKenzie
[ November 06, 2006: Message edited by: Cameron W. McKenzie ]
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I would also say that 2,3,4 are the correct options. In th first option Bar could be an Interface which is not a Object. The first option could be said, Foo has-a Bar.
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic