Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Protected constructors have the package private accessibility, i.e. only accessible from within the same package.
    However, because the superclass's constructor must run before the subclass's constructor, the subclass can specify
    which constructor the use, but only with proper accessibility as well as protected constructors.

    This means the a protected constructor cannot be used from another package, but a subconstructor can specify to "use"
    a protected superconstructor, for constructing the object it self, but not another object.

    Java Code:
    package thispackage;
    import anotherpackage.Superclass;
    class Subclass extends Superclass { //superclass have protected Superclass();
    public Subclass()
    {
        super(); //Allowed
        Superclass somethingelse = new Superclass(); //Not allowed
    }}
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  2. #22
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    It may also be good to add:

    Constructors are not static; you can not use access protected constructors (in another packages) from non-static methods or initialisers, either.

    Initialisers being to headless members:
    Java Code:
    class Class
    {
        {
            System.out.println("Hello from " + this.toString() + "'s initialiser.");
        }
    }
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  3. #23
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by Hibernate View Post
    Protected constructors have the package private accessibility, i.e. only accessible from within the same package.
    However, because the superclass's constructor must run before the subclass's constructor, the subclass can specify
    which constructor the use, but only with proper accessibility as well as protected constructors.

    This means the a protected constructor cannot be used from another package, but a subconstructor can specify to "use"
    a protected superconstructor, for constructing the object it self, but not another object.

    Java Code:
    package thispackage;
    import anotherpackage.Superclass;
    class Subclass extends Superclass { //superclass have protected Superclass();
    public Subclass()
    {
        super(); //Allowed
        Superclass somethingelse = new Superclass(); //Not allowed
    }}
    Excellent bro , thanks a lot for the above information . i clearly understood :)

    Quote Originally Posted by Hibernate View Post
    It may also be good to add:

    Constructors are not static; you can not use access protected constructors (in another packages) from non-static methods or initialisers, either.

    Initialisers being to headless members:
    Java Code:
    class Class
    {
        {
            System.out.println("Hello from " + this.toString() + "'s initialiser.");
        }
    }
    Constructors are not static; you can not use access protected constructors (in another packages) from non-static methods or initialisers, either.
    i could not understand the above line bro :( . yes ,constructors are not static , i know that , but i could not understand the below line

    you can not use access protected constructors (in another packages) from non-static methods or initialisers, either.
    Last edited by funkygarzon; 08-12-2011 at 11:48 AM.

  4. #24
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Just emphasising that protected constructor cannot be accessed at all from another package, exception from when specifying show constructor the superclass should use.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  5. #25
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by Hibernate View Post
    Just emphasising that protected constructor cannot be accessed at all from another package, exception from when specifying show constructor the superclass should use.
    thank s for the explanation bro , i can some how get your point by matching the my program that you have corrected . If you can please give me some links containing good java programs for practicing these king of stuffs and all the concepts in java . thanks in advance you bro :)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 2
    Last Post: 06-16-2011, 04:44 PM
  2. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  3. Replies: 4
    Last Post: 10-30-2009, 07:54 PM
  4. Eclipse "import not resolved" package problem?
    By spamsickle in forum New To Java
    Replies: 3
    Last Post: 08-24-2009, 11:44 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •