[Logo]
Forums Register Login
class accessibility
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
Hint : What about Inner class Outrer class relationship? Can't we use it?
 

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..
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)





still confused Ranchers help me out
 

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!
 

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?
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 ...
 

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...?

Abimaran...


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

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

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!
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 641 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jun 20, 2018 18:56:02.