• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Confusion Question

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
given these two source files
F1.java
Package forms;
Public class F1{ int type;}
F2.java
Package forms;
Class F2{
Int gettype(F1 ques)
{return ques.type;}
}
1. both compile successfully
2. Neither compile successfully
3. Only F1 compiles
4. Only F2 compiles
Totally confused with the question.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't mean to be picky, but nothing will compile. Java is case sensitive. Who knows if, in the exam, you may get a question that depends on you knowing just that?
Can you repost with the case corrected?
 
Trailboss
Posts: 24093
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did the question come from?
Tony's right. If that's the question verbatim, then neither will compile cuz there is no keyword "Package" although there is a keyword "package".
If we ignored case, I would say the answer is 3 because of the statement "return ques.type;"
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ignoring case (which applies to "Class", "Public" and "Int" as well as "Package"), then the correct answer is 1 - both compile successfully. (Yes I did it myself to be sure.) There's nothing wrong with the line "return ques.type;" - ques is declared as an instance of F1, and type is a member variable of class F1 which has package access. Since both classes are in the same package, this is fine. It's true that we often see only private variables (or public static final), but that isn't always the case.
 
paul wheaton
Trailboss
Posts: 24093
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doh!
Yer right. I just didn't see it.
Once again the compiler shows the proof.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Able to compile F1.java
Unable to compile F2.java
Got the compilation error as:
F2.java:3: Class forms.F1 not found.
int gettype(F1 ques) {
F2.java:4: Class forms.F1 not found in int gettype(forms.F1).
return ques.type;
2 errors.
Any help?
F1.java
package forms;
class F1{
int type;
}
<BOLD>F2.java<BOLD>
package forms;
class F2 {
int gettype(F1 ques) {
return ques.type;
}
}
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Siddhu- the problem is probably the form of the javac command you use. There are several ways to set it up, but try this:
<code>javac -d . F1.java F2.java</code>
The "-d ." tells the compiler to put the class files in appropriate subdirectories for the package they're in, which in turn allows the compiler to more easily locate the classes it needs later when a different file tries to access them. You need to compile F1 before (or simultaneously with) F2, or you need to have the .java files in package-specific subdirectories as well (again, so the compiler can find them).
 
Venkat01
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks every body for the reply and sorry for the typo. I promise to be more careful in future. Gentlemen, please understand that this is not deliberate.
Again thanks JIM, you have been wonderful. One small point, have you stopped answering posts in javacert.com. if so then there is no need to go there.
Byeeeee
 
Siddhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got a compilation error as when it tried to compile the below code.
Class1.java:2: Superclass Class1.Base of class Class1.Class1 not found.
public class Class1 extends Base {
I tried to compile using javac -d . Base.java Class1.java
<BOLD>
Base.java
package Base;
class Base {
protected void amethod() {
System.out.println("amethod");
}
}
Class1.java
package Class1;
public class Class1 extends Base {
public static void main(String[] args) {
Base b = new Base();
b.amethod();
}
}
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to specify the package that the Base class belongs to:
<PRE>public class Class1 extends Base.Base</PRE>
The classes are not in the same package.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...or you need to add the line
import Base.*; // (or Base.Base)
to the file Class1.java
Venkat01 - Thanks. I'm not yet planning to stop posting in javacert entirely, but I am planning to shift most of my posting here. Someone's got to look after Tony after all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic