Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

some doubts,please explain

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doubt 1:

1.class programmer{
2.programmer debug(){return this;}
3.}
4.class SCJP extends programmer{
5.//insert code here
6.}

Question: which inserted at line 5 will compile?

one of the answers to the question is
programmer debug(){return this;}

did not understand how did that happen as there is no overriding or overloading taking place.

Doubt 2:

1.class dog{}
2.class beagle extends dog{}
3.class kennel{
4.public static void main(String []args){
5.beagle b1=new beagle();
6.dog dog1=new dog();
7.dog dog2=b1;
8.//insert code here
9.}}

question:which inserted at line 8 will compile?
the answer to tis question was
beagle b2=(beagle)dog1;
beagle b3=(beagle)dog2;

did not understand this.Please help!!

Doubt 3:
The concept of var-arg is also not clear. i know it should come at the last in the method's argument.But could you please explain with an example.

Thanks.
 
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aditi,

doubt 1: why do you say there's no overriding taking place?

doubt 2: When doing a downcast, all the compiler does is check that Dog is in Beagle's inheritance hierarchy.

Gordy
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doubt 3:

The following link has some varargs examples
VarArgs Example
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a mistake in 1.
1. beagle b2=(beagle)dog1;
2. beagle b3=(beagle)dog2;

1. will cause a ClassCastException. It will compile OK because all the compalier is interested in when assessing casting is that the 2 classes are from the same hierarchy. (beagle and dog are) But at run time the real class of the object referenced by dog1 will be assessed. dog1 points to object of class dog. Can class dog be converted to beagle? NO, because it is trying to convert down to a subclass, rather than to superclass which is allowed.

2. Again no problem at compile time, as above. At run time JVM will see that in fact dog2 point to object of class beagle so the conversion is successfully done.

Regards
Katrin
reply
    Bookmark Topic Watch Topic
  • New Topic