• 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

Packages

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
In this pgm I thought answer is E.bcoz class A is not imported.Eventhough Class B inherited class A methods,to access class A methods we must include class A also.Am I right?If I am wrong,please clarify me.Thanks.

package xcom;
public class A {
// insert code here
}
package xcom;
public class B extends A {public void doB() { System.out.println("B.doB"); } }
import xcom.B;
class TestXcom {
public static void main(String[] args) {
B b = new B(); b.doB(); b.go();
}
}
Which, inserted at // insert code here will allow all three files to compile? (Choose all
that apply.)
A. void go() { System.out.println("a.go"); }
B. public void go() { System.out.println("a.go"); }
C. private void go() { System.out.println("a.go"); }
D. protected void go() { System.out.println("a.go"); }
E. None of these options will allow the code to compile.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kathir,

you only need to import classes that you use directly. Since in the example A is not used directly, there is no need to import it.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

import statements are NEVER needed.

The import statement is not something like an include statement in other languages.

import only saves typing.

If you adress class Bar from package foo then you
a) either type foo.Bar

or
B) import foo.Bar or import foo.*;
And then you have to type only Bar.

And when you are in the same package (foo) then you can omit both the import and the foo. before Bar.

Yours,
Bu.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is trying to check your Inheritence concept rather than package concept. Thus ans is A, B and D.
 
kathir vel
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Now I can understand.Thanks. But answer is only option B.bcoz Option A is only package level.Option D different package only within sub classes.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the answer will be only B iff the third file Testxcom will be in a different package. Though not explicitly mentioned the import statement is the clue.If in the same package then A, B, D will be correct.
 
reply
    Bookmark Topic Watch Topic
  • New Topic