The Artima Developer Community
Sponsored Link

Java Answers Forum
Controlling Access to Class Members

2 replies on 1 page. Most recent reply: Aug 10, 2008 5:20 AM by Zeinab S.Tehrani

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Zeinab S.Tehrani

Posts: 2
Nickname: zstehrani
Registered: Jul, 2008

Controlling Access to Class Members Posted: Jul 10, 2008 8:52 AM
Reply to this message Reply
Advertisement
1- can a constructor be private ? In what cases?

2-Imagine a method of a super class#1 in package#1 is private ,can sub classes of super class#1 in package#2 have access to that method?If not , then what about Inheritance ? If yes , then what about being private ?


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Controlling Access to Class Members Posted: Jul 11, 2008 1:05 AM
Reply to this message Reply
> 1- can a constructor be private ?

Yes

> In what cases?

In a situation where you require one instance of the class
throughout the lifetime of your application - as per Singleton Class.

e.g
public class A {
    private static A myA;
 
    private A() {
    }
 
    public static A getInstance() {
        if (myA == null)
            myA = new A();
        return myA;
    }
}


So everytime you want the singleInstance of A for this App.
you go:

   A myInstance = A.getInstance();


>
> 2-Imagine a method of a super class#1 in package#1 is
> private , can sub classes of super class#1 in package#2
> have access to that method?

No, unless you use Relflection - in which case you will
probably be creating a framework of somesort.

> If not , then what about
> Inheritance ?

What about it? the method is private, why should a subclass have access to it?

> If yes , then what about being private ?

Once again, what about it? If you are really eager to
access/call private methods from any class - whether its
a SubClass or a totally unrelated class, you can access/
call the method or even the private attributes via
reflection.

A lot of Frameworks generally use reflection - to give
the impression that things are just working magically.

Take a look at the BeanUtils class from Apache commons
to figure out how private properties of classes are
copied over.

Hope this answers your questions.

Zeinab S.Tehrani

Posts: 2
Nickname: zstehrani
Registered: Jul, 2008

Re: Controlling Access to Class Members Posted: Aug 10, 2008 5:20 AM
Reply to this message Reply
Hi
thanks a million for your answer , that was so helpful.
good luck

Flat View: This topic has 2 replies on 1 page
Topic: How to flatten the hook hierarchy when using Generic Template Class patter Previous Topic   Next Topic Topic: WHY is finding a JAVA/POS specialist so hard?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use