Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: about access modifiers (abstract and protected)

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default about access modifiers (abstract and protected)

    The combination of access modifiers such as abstract and protected are invalid combinations. If invalid please explain...

    Please answer ASAP.


    Thanks & Regards
    M.Satyanvesh


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: about access modifiers (abstract and protected)

    Class can only be public or default.
    And instance methods can be abstract and protected though. But you must need to implement this protected method in the very next concrete class(non abstract) or they will get private and will not be seen by any other hierarchy. So, this combination is valid.

  3. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: about access modifiers (abstract and protected)

    Class can only be public or default.
    Wait... I thought a class could be declared abstract, too. I also thought that if a class contained an abstract method, it must be declared abstract. Am I thinking of the wrong idea?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: about access modifiers (abstract and protected)

    Quote Originally Posted by snowguy13 View Post
    Wait... I thought a class could be declared abstract, too. I also thought that if a class contained an abstract method, it must be declared abstract. Am I thinking of the wrong idea?
    There is the difference between access and non-access modifiers. A class can only have public or default access modifier while abstract, final, native etc are non access modifiers. So, yes a class can be declared abstract too.
    I also thought that if a class contained an abstract method, it must be declared abstract
    I guess these things are like to known by any java developer, a class having an abstract instance method is of course abstract class.

  5. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: about access modifiers (abstract and protected)

    Ah, okay. Thank you.

    Is "protected" an access modifier, or is it categorized as a non-access modifier?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: about access modifiers (abstract and protected)

    protected is an access modifier, because it modifies how the method/variable can be accessed. abstract is not a access modifier, because it doesn't affect access:
    Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  7. The Following User Says Thank You to Tjstretch For This Useful Post:

    snowguy13 (February 1st, 2012)

  8. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: about access modifiers (abstract and protected)

    Ah, okay. Thank you.

    Is "protected" an access modifier, or is it categorized as a non-access modifier?
    Only public, private and protected keywords are access modifiers while there are four different access levels including three keywords and default access level.

  9. The Following User Says Thank You to Mr.777 For This Useful Post:

    snowguy13 (February 1st, 2012)

  10. #8
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: about access modifiers (abstract and protected)

    Hmm... I think it's a little bit odd that "protected" allows for more access than default!

    But thanks both of you for the help!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  11. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: about access modifiers (abstract and protected)

    Quote Originally Posted by snowguy13 View Post
    Hmm... I think it's a little bit odd that "protected" allows for more access than default!

    But thanks both of you for the help!
    Well, both work same except when it comes to the different packages. protected works in different packages only if there is inheritance while default doesn't. And in remaining all cases both work exactly similar.

  12. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: about access modifiers (abstract and protected)

    A class can only have public or default access modifier
    This is not entirely true. What about the following?

    public class OuterClass{
        public class PublicInnerClass{
     
        }
        private class PrivateInnerClass{
     
        }
     
        protected class ProtectedInnerClass{
     
        }
    }
    Last edited by copeg; February 1st, 2012 at 03:13 PM.

  13. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: about access modifiers (abstract and protected)

    "A class can only have public or default access modifier"

    This statement is completely true don't mix Class with Inner Class both are completely different, It will confuse others.

    As for as question is concerned

    Class: this combination is not valid because protected can not be applied to Class(Not inner class) at all.

    For methods there is no problem with this combination.

    For Inner class again this is not a problem.
    Below link could help you in access and non access.

    Java Access Modifier
    Java Non Access Modifier

    A regular inner class just like member of the outer class, like instance variable or methods. So all the modifiers that apply to these will be applied to inner class too like
    final, abstract, public, ..

  14. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: about access modifiers (abstract and protected)

    Quote Originally Posted by vivekanand30 View Post
    ...don't mix Class with Inner Class both are completely different, It will confuse others.
    Fair enough...

  15. #13
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: about access modifiers (abstract and protected)

    Quote Originally Posted by copeg View Post
    This is not entirely true. What about the following?

    public class OuterClass{
        public class PublicInnerClass{
     
        }
        private class PrivateInnerClass{
     
        }
     
        protected class ProtectedInnerClass{
     
        }
    }
    Well, i was supposed to talk about non-nested or non-inner classes. Well, thanks copeg anyways for pointing this out as well. Now, Any non-nested or non-inner class can have only public or default access. This seems fine now.

Similar Threads

  1. GUI error: is not abstract and does not override abstract method
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2011, 01:26 PM
  2. Abstract
    By mamawia in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2011, 07:16 AM
  3. Using Protected Methods, in a Java Library
    By WACman in forum Object Oriented Programming
    Replies: 2
    Last Post: March 10th, 2011, 05:42 PM
  4. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM
  5. Replies: 2
    Last Post: June 13th, 2009, 01:44 AM