• 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

Class declarations

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following class declaration(s) is(are) true?
A.import java.awt.Button;
import java.lang.Runnable;
public class MyClass extends Button implements Runnable{
public void run(){

//Some valid Code
}
}
B.import java.awt.event.*;
import java.applet.Applet;
public class MyApplet extends Applet,WindowAdapter{
public void windowClosed(WindowEvent we){

//Some Valid Code
}
}

C.import java.lang.Runnable;
public class MyClass implements Runnable{
public void run(){
int i = 10
System.out.println("i = "+i);
}
}
D.import java.awt.event.*;
public class MyClass extends WindowAdapter
implements WindowListener{
public void windowClosed(WindowEvent we){
//Some Valid Code
}
}
I have answered A and C, but the site says answer is A and D.
The Choice D, i think is wrong because when we are implementing an Interface then all its methods are to be overridden other wise the class is to be declared abstract
Can anyone say why choice C is wrong, i dont see anything wrong in it.
Please clear
Thanks in advance
kareem
:roll:
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C is wrong because this line:
int i = 10
has no semicolon after it.
D is fine. You are extending WindowAdapter,which is a concrete class, and contains implementations for all the methods in the WindowListener class, which WindowAdapter implements.
It's redundant to declare that MyClass implements WindowListner, because it already does so indirectly via its superclass WindowAdapter. However, even though it's redundant, it's not an error.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in choice C: If you look carefully, a semicolon is missing on line int i=10
This is mean, though !
in choice D: MyClass extends WindowAdapter which already implements WindowListener, and thus, MyClass also implicitely implements WindowListener by inheriting WindowAdapter methods. There is no problem with that code.
Rob,
WindowAdapter is an abstract class although no methods in it are abstract. This is done that way so that developer actually subclass WindowAdapter.
[ March 15, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently, this is obvious to everyone but me!
Why is choice B wrong?
Thanks.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class cannot extend multiple classes !
 
Anup Engineer
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats the worst I have missed, as yet!
Thanks.
 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey that was pretty silly i missed the ;
also thanks for reminding me that Adapter classes are actually implementations of the Listener interfaces
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the real exam, you won't need to make sure that no semicolons are missing because the focus is mainly on Java concepts and not on syntax issues.
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic