Win a copy of Modern frontends with htmx this week in the Spring 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:
  • 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

JQplus Questions

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q.1
class A
{
void setColor(byte i) {System.out.println(""+i);}
public static void main(String args[])
{
A a = new A();
byte b = 30;
a.setColor(b);
a.setColor(30);
}
}
This code doesn't compile.It gives the error Incomptiable type for a method.Explicit cast needed to convert an int to byte.
I don't understand why does it gives this error. The range for a byte is 128.So it should accept it.What I am miissing.
Please explain.
Q.2
Which of the following statements regarding inner classes are true?

A. A nonstatic inner class may have static members.
B. Anonymous inner classes cannot have any extends or implements clause.
C. Anonymous inner classes can only be created for interfaces.
D. Anonymous inner classes can never have initialization parameteres.
E. Anonymous inner classes cannot be static.

The correct answer given is A,B,E.
I agree A&E. But what about B. An Anonymous inner classes can have extends or imnplements kry word.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B and D are the only correct choices.
non-static inner classes cannot have static members.
anonoymous inner classes cannot have initialization of member variables.
anonymous inner classes cannot explicitly use the extends or the implements clause.
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why B is correct.Annonymous inner classes can have extends and implements keyword.Please explain
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friend
About your first question
There is no byte and short literals in java.(Literals are the values assign to datatype)
So when you are assigning
byte b = 30;
you are assigning a integer literal which is in range of byte
(-128 to 127)
But while passing a integer literal to a function which is taking a byte value causes a c/t error.
One intresting thing.
int i =10;
byte b =i;
Causes C/T error.
but
final int i =10;
byte b =i
Works perfectly.
Because in second case, it is as good as saying
byte b =10
as i is final ...constant).

For your second question
you read a topic of Anonymous inner classes once again .Your doubts will be cleared(read either RHE or Mughal)
bye
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
Thanks a lot swati for your explanation. But i don't understand one thing. When you pass assign a literal or an int to byte and if it is in the range of the byte it accepts the value. But when we assign the same literal to a function of same parameter byte why doesn't it accept the value.
please explain .
Thanks in advance.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys
I am not able to justify swati's answer.
This is my view
byte b = 10; // is a valid decleration
The Compile time error occurs when the
setColor(30) call is made
since 30 is a integer and the
setColor(byte i) doesn't have an explicit type cast to int.
Please compile the code and see it for yourself's
--
Venkat
 
Enthuware Software Support
Posts: 4798
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is actually not much to it. It's the way the language is designed. The rule is, implicit narrowing does not happen for method parameters. And that's why although 30 can easly fit into a byte the compile does not accept it.
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
natchit
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
Thanks for letting me know that implicit narrowing is not allowed for method parameters.
Swati, your explanation was not cent precent correct.
byte b=10 is acceptable since -128<10<127.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Shabbir
The answer to your question regarding anonymous class is as follows:
The very syntax for construction for an anonymous class that implements an interface or extends a class is
---- new <interface name> () {class declaration}
---- new <super class name> (optional parameters) {class decl}
note that there is not "extends" or "implements" keyword.
reply
    Bookmark Topic Watch Topic
  • New Topic