Results 1 to 7 of 7
  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default protected modifier ? Constructors

    Please help me

    Why do we use protected modifer to an abstract class,,,

    ..here is the sistuation,,,,,
    I have an Abstract class called GeometricObject...

    do i have to make my constructor Protected ,, or Public... ??
    And WHAT is THE DIFFEREnce ??
    Please explain in detail what is the difference between these two???:confused:

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Public means that anything can access that constructor, method or variable. Protected means that it is only visible to a subclass.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    ... to a subclass or another class in the same package.

    An abstract class cannot be instantiated. Another class must extend it. The extending class must call at least one of the abstract class' constructors. If the constructor is private, then the extending class cannot find the constructor.

    There is little reason to make an abstract class' constructor anything but public, since only extending classes can call the constructor anyway. In general, you should make constructors public unless you have a specific reason for hiding them.
    The Java Tutorial. Read it.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Steve11235 View Post
    There is little reason to make an abstract class' constructor anything but public
    I beg to differ: there is little reason to make an abstract constructor anything but protected because (as you wrote) other classes have to extend this abstract class to create a concrete class and (more important imho) an extending class can't reduce the visibility of an inherited method or constructor. What if an extending class wants to have a protected constructor if the abstract base class has a public constructor already? It can't do it while the protected scenario still allows such extension.

    kind regards,

    Jos

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Good point. The implementer then has the option to make the implementing constructor either protected or public. If this is a company policy based on that line of reasoning, it makes good sense. On the other hand, if this is not a standard, it could be somewhat confusing, in that it could imply that some restriction *should* be retained.
    The Java Tutorial. Read it.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Steve11235 View Post
    Good point. The implementer then has the option to make the implementing constructor either protected or public. If this is a company policy based on that line of reasoning, it makes good sense. On the other hand, if this is not a standard, it could be somewhat confusing, in that it could imply that some restriction *should* be retained.
    If you look at the core classes that start with "Abstract" quite a bit of them have protected constructors; I don't know why the others have public constructors. Personally I find it a bit inconsequent. Of course there is no restriction involved because a subclass of an abstract class can always widen the access rights of the inherited methods/constructors.

    kind regards,

    Jos

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Just thought I ought to point out that constructors aren't inherited.

    This (assume the classes are in separate files):

    Java Code:
    public abstract class Blah {
        private Blah () {
        }
    
        protected Blah (int i) {
        }
    }
    
    public class BlahImpl extends Blah {
        public BlahImpl () {
            super(1);
        }
    
        private BlahImpl (int i) {
            super(1);
        }
    }
    is perfectly valid.

    The private empty constructor has had it's visibilty increased, and the paramterised one has had it reduced...because they aren't inherited.

Similar Threads

  1. default access modifier
    By vishnugch in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 01:48 PM
  2. why cant a top most class has staic modifier
    By srikantha in forum New To Java
    Replies: 4
    Last Post: 08-06-2009, 07:03 PM
  3. Replies: 24
    Last Post: 05-18-2009, 11:23 AM
  4. Regarding default access modifier?
    By makpandian in forum New To Java
    Replies: 2
    Last Post: 03-14-2009, 09:21 AM
  5. why we use public access modifier?
    By vichet in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 08:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •