Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Unexpected behavior

    Why am I getting a compiler error when accessing protected method of TestAnimals from its subclass method doit(), which is against the concept of protected modifier which says even if the subclass is in different package than the original then subclass can access the protected method?

    TestAnimals.java
    Java Code:
    package pack1;
    
    public class TestAnimals {
    	public static void main(String[] args)
    	{
    		
    	}
    	
    	protected void doSomething()
    	{
    		
    	}
    }
    Horse.java
    Java Code:
    package pack2;
    
    import pack1.TestAnimals;
    
    public class Horse extends TestAnimals
    {
    	public void doit()
    	{
    		TestAnimals t=new TestAnimals();
    		doSomething();
    		t.doSomething();
    	}
    }

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

    Default Re: Unexpected behavior

    WHAT error?

  3. #3
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Unexpected behavior

    The method doSomething() from the type TestAnimals is not visible

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Unexpected behavior

    I have suggested this before and you have not responded to my suggestion so I will make it again. Why have you not gone thru the tutorials linked to in my signature? Instead of asking these questions on the forum you could have figured them out for yourself. The forum is here to help but you really should attempt to figure some of this stuff out on your own. The only way to learn Java is to read about it and practice writing code.

    Regarding this question: Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,279
    Rep Power
    6

    Default Re: Unexpected behavior

    Quote Originally Posted by jim829 View Post
    Why have you not gone thru the tutorials linked to in my signature?
    Because people keep answering. Perhaps we should just stop doing that, for the guy's own benefit.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Unexpected behavior

    I have read the whole book of complete reference in Java. It says that a protected member can be accessed also by subclasses in different package. So why is the compiler throwing an error?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Unexpected behavior

    Here, I've done your research for you: Chapter*6.*Names

    I won't do it every time though, so you'd better bookmark the JLS.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,279
    Rep Power
    6

    Default Re: Unexpected behavior

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    I have read the whole book of complete reference in Java. It says that a protected member can be accessed also by subclasses in different package. So why is the compiler throwing an error?
    Because you misunderstood the book, which SHOULD have triggered you to do further reading from different sources to gain a more complete understanding. If you don't start to realize that you MUST do this, I don't see much of a future for you in development. This is every day life. I've done this trick for 15+ years now and I still need to research stuff daily. No problem, because research is easy - all you have to do is make effort.

    btw: you only read the book once? When I was learning Java (or C++, or Pascal, or Assembly) I was reading and re-reading in a book -every evening- for several months, until it all stuck.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Unexpected behavior

    Quote Originally Posted by gimbal2 View Post
    btw: you only read the book once? When I was learning Java (or C++, or Pascal, or Assembly) I was reading and re-reading in a book -every evening- for several months, until it all stuck.
    The latest version of "The C++ Programming Language" is completely unreadable because of that proportional spaced font used for C++ code; it looks like the gibberish in this forum where people forget to use the code tags ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,279
    Rep Power
    6

    Default Re: Unexpected behavior

    Quote Originally Posted by JosAH View Post
    The latest version of "The C++ Programming Language" is completely unreadable because of that proportional spaced font used for C++ code; it looks like the gibberish in this forum where people forget to use the code tags ...
    Is that just a general rant or does it have any special meaning to this thread that I am too stupid to see?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. unexpected NullPointerException
    By dawnMist in forum New To Java
    Replies: 8
    Last Post: 11-07-2012, 06:40 PM
  2. Unexpected Output
    By ankiit in forum New To Java
    Replies: 20
    Last Post: 01-06-2012, 06:48 PM
  3. Replies: 3
    Last Post: 06-28-2009, 08:18 PM
  4. unexpected type
    By one in forum New To Java
    Replies: 13
    Last Post: 01-20-2009, 10:32 AM
  5. Unexpected Error
    By Mir in forum New To Java
    Replies: 10
    Last Post: 07-07-2008, 08:57 PM

Posting Permissions

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