• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Mock Exam Qn doubt

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this Qn from the site www.examulator.com

code
--------------------------------
interface Skeleton{
public char getSize(int iSize);

}


public class Huskisson implements Skeleton{
int iSize=99;
public static void main(String argv[]){
new Huskisson();
}
Huskisson(){
int iSize=1;
System.out.println(getSize(iSize));

}

public int getSize(int iSize){
return iSize;
}
}
------------------------------------------
The output is Compiler Error. I thought the output will be compilation with output 1.
Can somebody explain?
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile and check it out buddy.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Does your class implement the Skeleton interface well?

Hint: interface says the method to return char and you have implemented
it with int return type. return type must be same for overriding method
of a class, or implementing method of interface.

Covariant return: The overriding method may return same or subclass
of what the overridden method returns.

EDIT:
Covariant return doesn't apply to primitives. char can't be a return
type where int is required.


Thanks,
[ July 03, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helo

I feel that the iSize variaable has already been defined as an instance variable and as a local variable of the constructor
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Juwonlo Ibigbami:
Helo

I feel that the iSize variaable has already been defined as an instance variable and as a local variable of the constructor



Both have different scope. One is instance variable and another is constructor
local variable. No problem in this regard.


Thanks,
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even if the method were implemented correctly, the output would not be 1.
There's shadowing occuring here.

By the way, Chandra: covariant returns an primitives ?

Yours,
Bu.


---
know this button?
reply
    Bookmark Topic Watch Topic
  • New Topic