• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
Wanna see my flashlight? How about this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic