开发者

Question in inheriting and hiding methods & classes

开发者 https://www.devze.com 2023-01-14 23:27 出处:网络
I have questions about inheriting(extending) methods from classes and hiding some classes and methods from the main :)

I have questions about inheriting(extending) methods from classes and hiding some classes and methods from the main :)

Assume that I have class A & class B. Class B has method 1. and I need to use that method(method 1) in class A, but in the same time I need to hide that class(Class B with its methods) from the main.

I mean I want to deal with method 1 only from class A(or any other additional class, let's say Class C) I don't want it to be seen in the main. I don't want to give the possibility for the main to create an instance from Class B to use method 1. I need to do that, because when I create a jar file for my classes, there are classes that have methods that I don't want them to be shown. These classes I used them only when I did create the ja开发者_C百科r file.

How I can hide these classes and methods ?? I hope my explanation is clear...

Please don't tell me to do these things, because I thought of it, and it doesn't works : :)

1) to use private for the methods that I don't want them to be shown, because I can't do that. since, it's a separated classes not in the same class, and when I put private near the function , I can't use it at all, only it will works with the same class.

Also,

2) To put all the functions in the same class, Because I need to organize my code with classes with a separated files :)

Thanks alot Guys ....


You can solve your problem by using different packages for your classes:

Put your class containing the main() method into package m. Then put class A in package a and make it public. Put class B in package a, too and make it only package protected (skip the public key word).

Now you can access class A from your main(), but you can't access class B.


Most probably you want package protected methods. If you omit the visibility modifier (no public, protected or private) the method is visible for all classses in the same package.


I think you want protected access; these methods will be visible, callable, and overridable by subclasses, but hidden from other classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消