-1

How can I create a member that should be available only to my sub classes in java ?

Rajesh
  • 119
  • 1
  • 8
  • 4
    Considering that you know which tags to use, what is the issue? https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – user3707125 Jan 27 '16 at 21:52
  • 1
    Possible duplicate of [Which Java access modifier allows a member to be accessed only by the subclasses in other package?](http://stackoverflow.com/questions/21133124/which-java-access-modifier-allows-a-member-to-be-accessed-only-by-the-subclasses) – John_West Jan 28 '16 at 00:06
  • Here's a good table for future reference: http://stackoverflow.com/a/33627846/276052 – aioobe Mar 08 '16 at 21:01

3 Answers3

1
static class XX {
    private static int p = 10; //p is only accessible to TT

    static class TT {
        static public int getT() {
            return p;
        }
    }

}
Rohit Kasat
  • 300
  • 3
  • 13
  • thanks for your reply. this looks pretty close of what i am looking for. But guess we can use without static too. – Rajesh Jan 27 '16 at 22:44
1

Assuming your child classes are subclasses, any public variable is accessible to them. If you wish to access private variables from a subclass, than you have to change them to protected.

Bifz
  • 403
  • 2
  • 9
  • thanks for your reply. I was curious to know, Is there any way that members can be only visible to subclasses ? – Rajesh Jan 27 '16 at 22:41
  • @Rajesh Isn't that the same question? Yes, use `protected`. – Bifz Jan 27 '16 at 22:57
  • protected will be visible to all the classed in the package. I dont want that. Member should be "visible only" to the subclasses. – Rajesh Jan 27 '16 at 23:01
  • There is no way to make it visible to subclasses without making it invisible to package classes. But you can manage your packages the way you like, putting only subclasses in it, for instance. – Bifz Jan 27 '16 at 23:22
  • Yes, thats one way we can do it. Thanks! – Rajesh Jan 28 '16 at 00:06
0

Assuming that when you say "variable", you are talking about a member of some class. E.g.,

class Parent {
    SomeType myVariable;
    ...
}

Assuming that when you say my child classes, you are talking about the extends relationship in Java. E.g.,

class Child extends Parent {
    ...
}

In that case, you can't have exactly what you ask for, but maybe you can have what you need.

If you declare the member to be protected, then it will be visible in any child class, but it will also be visible in any other class that belongs to the same package, regardless of whether the other class is a child or not.

class Parent {
    protected SomeType myVariable;
    ...
}

IMO, there's not much use for protected. It serves as a suggestion that other programmers should not try to use it in a class that does not inherit from the parent, but so what?

If you see a private member in some class, then you are assured that no code outside of the source file where you see it can possibly depend on it.

If you see a member with default access (i.e., neither public, nor private, nor protected), then you know that any code that depends on it must be declared in the same package. Nothing prevents some other programmer somewhere in the world from declaring his own code in your package, but if he/she does, then she or he is being stupid, and when you release a new version that breaks his/her application, that's not your problem.

If you see a member with public or protected access, then you have to assume that people you don't know, and people you will never meet are depending on you to not change it.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • thanks for your reply. I was curious to know, Is there any way that members can be only visible to subclasses ? – Rajesh Jan 27 '16 at 22:38
  • @Rajesh, No. The closest you can come is to declare the variable `protected`. A `protected` variable in `public class P` can be seen in any class that is declared in the same package, and it can be seen in any `class C extends P` regardless of the package. – Solomon Slow Jan 28 '16 at 16:53