Results 1 to 7 of 7
  1. #1
    bluepratz is offline Member
    Join Date
    Aug 2010
    Location
    Mumbai, India
    Posts
    4
    Rep Power
    0

    Default Protected access modifier in Java

    Hello,
    If I am not wrong then the method with protected access modifier can be accessed by the same package and the sub-class outside the package. But I have problem regarding the same

    I have two packages p1 and p2, but when I try to access the method (protected) of package p1 from package p2, the compilers gives an error that it is not visible or it is protected. I am using Eclipse IDE, even I have checked without using Eclipse, I can not access the method from package p1

    Here is the first package p1 and class ClassTest1 with method DisplayName1
    Java Code:
    package p1;
    public class ClassTest1 
    {
    	protected  void display1()
    	{
    		System.out.println("This is ClassTest1");
    	}
    }

    Here is the second package p2 with two classes ClassTest2 and MainTest. ClassTest2 has method DisplayName2.
    Java Code:
    package p2;
    import p1.*;
    
    public class ClassTest2 extends ClassTest1 
    {
    	protected void display2()
    	{
    		System.out.println("This is ClassTest2");
    	}
    }
    And
    Java Code:
    package p2;
    
    public class MainTest 
    {
    	public static void main(String[] args) 
    	{
    		
    		ClassTest2 ct = new ClassTest2();
    		ct.display2();
    		ct.display1(); //i get an error on this line, the method display1 is not visible to the class
    	}
    
    }
    Any help is appreciated. Thank you.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Already answered in your Dani-Web post.

  3. #3
    bluepratz is offline Member
    Join Date
    Aug 2010
    Location
    Mumbai, India
    Posts
    4
    Rep Power
    0

    Default

    Already answered in your Dani-Web post.
    I had asked it on both the forums so that I can get the query solved quickly. I think rather that checking for content duplication you guys should concentrate on replying to the queries. By the way my query is not solved, I still have issues in it, but now I am not going to post it here as you just display the link.
    So, here is the link to the post Protected access modifier in Java - Java, this is not solved yet, if you can please.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You could however, mention that it is posted twice, as nobody likes wasting their time making "suggestions" that have already been made. And, yes, the question is already solved.

  5. #5
    bluepratz is offline Member
    Join Date
    Aug 2010
    Location
    Mumbai, India
    Posts
    4
    Rep Power
    0

    Default

    mention that it is posted twice
    I do not think that I have to follow some rules here.

    as nobody likes wasting their time making "suggestions" that have already been made.
    From the users point of view, a user can post the same query in 10 places, I do not think that this should matter anyone. I think forums are like helping others, but in your above sentences I can sense pure business.

    yes, the question is already solved.
    When I was replying to this query, my question was not solved. Yes I guess it is now solved, but still I have not understood why we can not instantiate an object of the base class to access the protected method from the base class. Any more help is appreciated. Thank you.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Quote Originally Posted by bluepratz View Post
    I do not think that I have to follow some rules here.
    You do if you actually want someone to help you.

    Quote Originally Posted by bluepratz View Post
    From the users point of view, a user can post the same query in 10 places, I do not think that this should matter anyone. I think forums are like helping others, but in your above sentences I can sense pure business.
    We're all volunteers, many with jobs to do ourselves. It's not "pure business" (whatever that means), but plain good manners to inform us that you have cross posted to other forums. We don't like to find out we've wasted our time answering something that has already been dealt with elsewhere.

    Quote Originally Posted by bluepratz View Post
    When I was replying to this query, my question was not solved. Yes I guess it is now solved, but still I have not understood why we can not instantiate an object of the base class to access the protected method from the base class. Any more help is appreciated. Thank you.
    You get an error when calling display1() because MainTest is not in the same package as ClassTest1. That's it, pure and simple.

  7. #7
    bluepratz is offline Member
    Join Date
    Aug 2010
    Location
    Mumbai, India
    Posts
    4
    Rep Power
    0

    Default

    Hey guys, forget it, I apologize if I am wrong somewhere. Finally happy programming.

Similar Threads

  1. protected modifier ? Constructors
    By javanew in forum New To Java
    Replies: 6
    Last Post: 04-06-2010, 11:59 AM
  2. default access modifier
    By vishnugch in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 12:48 PM
  3. Regarding default access modifier?
    By makpandian in forum New To Java
    Replies: 2
    Last Post: 03-14-2009, 08:21 AM
  4. Ask for help on Java access to protected methods
    By fangzhong in forum Advanced Java
    Replies: 3
    Last Post: 02-17-2009, 01:50 PM
  5. why we use public access modifier?
    By vichet in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 07:04 AM

Tags for this Thread

Posting Permissions

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