This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""protected"  error" Watch ""protected"  error" New topic
Author

"protected" error

newtojava
Greenhorn

Joined: Aug 10, 2000
Posts: 6
Hi:
Could anyone pls explain why I'm getting the following error.
My classpath is set to : .;G:\CorejavaBook
Then I created a package under G:\Corejavabook\Corejava --->
package corejava;
public class myclass
{
public myclass()
{
System.out.println("New Object of myclass:" );
}
protected void bite()
{
System.out.println(" In bite");
}
}
When I write the following Class to access the Protected func--->
import corejava.*;
public class cake extends myclass{
public cake()
{
System.out.println("cake constructor");
}
public static void main(String[] args)
{
myclass x = new myclass();
x.bite();
}
}
I get a error "Can't access protected method bite".I'm new to Java, could you pls explain why can't I access the protected method. From what is in the book, protected is accessible in subclass in other packages.
Thanks in advance.
newtojava
Lancy Mendonca
Ranch Hand

Joined: Aug 08, 2000
Posts: 54
The class that you have created is an instance of myClass. In myClass protected is equivalent to a private object. Hence you cannot access it.
if your code read
public static void main(String[] args)
{
cake c = new Cake();
c.bite(); // It will work.
//However the following will compiler error
MyClass m = new MyClass();
m.bite();
// Or
MyClass m = new Cake();
m.bite();
This is surely a tricky thing to understand for a beginner. If U need more explaination I will be happy to explain further.
}


Sun Certified Java Programmer<BR>Oracle Certified DBA
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
A further explanation would be great. There seems to be many questions on this matter lately. Also when it comes to packages and classpaths and when and where problems will arise. I thought I understood it ok, but when I tried looking at some of these issues, I got more confused.
Thanks
newtojava
Greenhorn

Joined: Aug 10, 2000
Posts: 6
Hi Lancy
Thanks for the reply.Could you pls explain some more on this topic.This is educational and more helpful than reading a book.
Thanks a lot in advance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "protected" error
 
Similar Threads
importing multiple classes from a package
jq+ Question ID :957672686580
Protected Trouble
classes inside methods
protected access from another package