• 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

Marcus Green's exam #2

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a question from Exam #2 from Marcus Green......
Q. You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled Base.java
//Base.java
package Base;
class Base{
protected void amethod(){
System.out.println("amethod");
}//End of amethod
}//End of class base
package Class1;
//Class1.java
public class Class1 extends Base{
public static void main(String argv[]){
Base b = new Base();
b.amethod();
}//End of main
}//End of Class1
1) Compile Error: Methods in Base not found
2) Compile Error: Unable to access protected method in base class
3) Compilation followed by the output "amethod"
4)Compile error: Superclass Class1.Base of class Class1.Class1 not found
The answer given is 4. What I do not understand is Why???
The method 'amethod();' in the 'Base' class is 'protected'.... So shouldn't it be possible to access this method from any subclass, even if the subclass lies in some other package???
Based on this, shouldn't 3. be the correct answer??? I copiled and executed the code but found that 3 is NOT the right answer..... can't figure out why???
Can anybody throw some light on this???
Thanks,
Shafeeq
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at Answers for questions 1,2 at this link. http://www.absolutejava.com/articles/test-your-java-knowledge.html. Explains it very well.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method is protected, not the class Base. The Base class is not visible in any other package so obviously its method won't be visible even if its protected.
Chris
 
Shafeeq Sheikh
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sat Man.....
The link doesn't work.......
Anybody???
 
Shafeeq Sheikh
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris.....
The class can never be protected..... In fact apart from an inner class, no class can ever be protected....
Now if what you say is true, then what is the use of the protected keyword???
:confused
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the answer is nothing to do with the Access Modifier.this ClassDefinitionNotfound error is Generated by class Loader.
Since in Class1.java Source file no imports are specified ,the compiler will look for definition Base.class .Since it cannot find any Base.class file in the package class It will generate Compiler error Saying Class1.Base is not FOund.So the answer is 4.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the answer is nothing to do with the Access Modifier.this ClassDefinitionNotfound error is Generated by class Loader.
Since in Class1.java Source file no imports are specified ,the compiler will look for definition Base.class in the package class1.Since it cannot find any Base.class file in the package class1 It will generate Compiler error Saying Class1.Base is not FOund.So the answer is 4.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to remember there are actually 2 access modifiers you need to evaluate:
1) Access modifier for the class
2) Access modifier for the methods/variables within the class
Even though the methods or variables have protected modifiers you still need to know if the class that contains the methods/variables can be accessed or not. In this case, the class has default modifier. If you look at the top of the first class it has a package defined, so unless the 2nd source file has the same package defined you will not be able to access the 1st class because they are considered in different packages (even though they are in the same directory).

 
reply
    Bookmark Topic Watch Topic
  • New Topic