• 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 accessibility

 
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys
in interview someone asked me that lets say there are so many classes in the same package like A,B,...Z
make a strategy that the methods of B class can only be accessed by A class but no others can use them
can someone explain who can we achieve this
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint : What about Inner class Outrer class relationship? Can't we use it?
 
akhter wahab
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:Hint : What about Inner class Outrer class relationship? Can't we use it?


thanks Abimaran but i also read this in interview questions on internet but i don't have a complete justified answer that i really needed how we can use inner class for doing this..
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check the following program. Anonymous subclass of A cannot be accessed from any other classes.It can access using A's reference variable only.Please correct me if I am wrong




Please excuse me... i have a doubt in this progam... if i omited the public keyword from class B and saved in some other name say test.java, it raises the following run time exception. i didnt understand the cause. Anyone please help me...



Exception in thread "main" java.lang.NoClassDefFoundError: a (wrong name: A)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)





 
akhter wahab
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still confused Ranchers help me out
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manjusha Muraleedas wrote:Please check the following program. Anonymous subclass of A cannot be accessed from any other classes.It can access using A's reference variable only.Please correct me if I am wrong



First, a public class in java should be saved in that class name, in your case, since class B is public, you should saved the file with the name as B.java, otherwise change the public class name, as the file name!

And other problem with that anonymous inner class A. As you said, you want to change the pop() method access modifier in the anonymous inner class. That anonymous inner class is a sub class of the class A, so you should follow the overriding rules. Otherwise Compilation Error will occur!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

akhter wahab wrote:still confused Ranchers help me out


Don't you get the point?

Let's help, What do you say about the below code? Is it legal/possible?
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you abhiraman for the answer...I was wrong...

First, a public class in java should be saved in that class name, in your case, since class B is public, you should saved the file with the name as B.java, otherwise change the public class name, as the file name!



I am saying aboout making Class B a default class.



Now I saved the file as Test.java. Am getting runtime exception ...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manjusha Muraleedas wrote:Thank you abhiraman for the answer...I was wrong...(


Who is he?

Manjusha Muraleedas wrote:
Now I saved the file as Test.java. Am getting runtime exception ...



Please give detail. NoClassDefFoundError...?
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran...


This is the result.
C:\New Folder>javac Test.java

C:\New Folder>java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manjusha Muraleedas wrote:
Abimaran...
C:\New Folder>java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test



You should run the program as C:\New Folder>java B

Could you find why is that? It's better for you!
reply
    Bookmark Topic Watch Topic
  • New Topic