Home arrow static arrow Java Programming [Archive] - Sample java program
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Sample java program
This topic has 12 replies on 1 page.

Posts:44
Registered: 4/14/04
Sample java program  
Aug 3, 2004 10:53 PM



 
Respected sir/ madam
I have one doubt related to the following java program...I didnt understood why the error is occuring in the following program..Can any body please tell me why this error is occuring ,might be I am weak at my basics ....

package p1;
public class A
{
protected int i=10;
public int getI()
{
return i;
}
}
// now in another package ...
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i=a.i * 2;
}
public static void main(String [] args)
{
A a=new B();
B b=new B();
b.process(b);
System.out.println(a.getI());
}

}
It is giving me compile time error ....Please help
Waiting for your reply.
Thanks and Regards
 

Posts:18
Registered: 3/4/03
Re: Sample java program  
Aug 3, 2004 11:05 PM (reply 1 of 12)



 
please say more detailed what the error is. what does the compiler tell you?
 

Posts:44
Registered: 4/14/04
Re: Sample java program  
Aug 4, 2004 8:42 PM (reply 2 of 12)



 
The compiler which gave me the error is as folows

p2/B.java [7:1] i has protected access in p1.A
a.i=a.i * 2;
^
p2/B.java [7:1] i has protected access in p1.A
a.i=a.i * 2;
^
2 errors
Errors compiling B.
 

Posts:37,103
Registered: 3/30/99
Re: Sample java program  
Aug 4, 2004 10:04 PM (reply 3 of 12)



 
If you made the variable protected, you should know what protected means. If you don't know, then look for it in one of the following:

[url=http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com]http://javaalmanac.com[/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]The Java Developers Almanac[/url].

[url=http://www.jguru.com]jGuru[/url]. A general Java resource site. Includes FAQs, forums, courses, more.

[url=http://www.javaranch.com]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

Bruce Eckel's [url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url] (Available online.)
Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url]
Bert Bates and Kathy Sierra's [url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance]Head First Java[/url].
 

Posts:5,965
Registered: 5/17/03
Re: Sample java program  
Aug 4, 2004 10:17 PM (reply 4 of 12)



 
The i variable is declared protected. This means it's accessible within the same package only. If you declare it public it can be reached from anywhere.
 

Posts:37,103
Registered: 3/30/99
Re: Sample java program  
Aug 4, 2004 10:25 PM (reply 5 of 12)



 
The i variable is declared protected. This means it's
accessible within the same package only.

...and by subclasses.
 

Posts:45
Registered: 11/24/02
Re: Sample java program  
Aug 4, 2004 11:41 PM (reply 6 of 12)



 
The i variable is declared protected. This means it's
accessible within the same package only. If you
declare it public it can be reached from anywhere.

True, but NOT GOOD. You should not declare your variables public as this violates the principles of encapsulation and OOP. A class should never expose its data to direct manipulation from external sources. Data should be declared private - or at the very least protected.

If you would like to modify the variable i in class A then create a public method to do it for you eg.
public void multiplyI(int multVal){    i = i * multVal;}

Now, if I want to multiply i by 2 I can write:
  a.multiplyI(2);

If you can affect a variable directly then you have design problems
Ben
 

Posts:5,965
Registered: 5/17/03
Re: Sample java program  
Aug 4, 2004 11:48 PM (reply 7 of 12)



 
Data should be declared private - or at the very least
protected.

I agree but protected was the problem in this case wasn't it -:)

If you can affect a variable directly then you have
design problems
Ben

No rule without an exception. For example a low level class like Point3D could very well expose its variables without beeing called a design flaw,
public class Point3D {   public double x,y,z;}
 

Posts:23
Registered: 7/29/03
Re: Sample java program  
Aug 5, 2004 1:12 AM (reply 8 of 12)



 
Hi!

don't get confused.
have a look on Packages & access specifiers, u'll know the error made.
 

Posts:44
Registered: 4/14/04
Re: Sample java program  
Aug 5, 2004 4:27 AM (reply 9 of 12)



 
Hi all
According to Khalid Mughal for Sun Certifed Java Programmer
it is said that not only protected members are accessible to all classes within a package BUT also to all subclass of that class irrespective whether the subclass is in the same package or somewhere else........
I have extended the class A ....
Thats where I am confused.....
In one of the mock exam it is said that since u r not providing the implementation of class A in class B which is a subclass of class A thats why you cannot access the protected variable i directly..
I didnt understood the above explaniation which they were trying to explain....
Please help !!
Waiting for your reply..
 

Posts:196
Registered: 6/29/04
Re: Sample java program  
Aug 5, 2004 4:50 AM (reply 10 of 12)



 
When u r doubting,
the JSL ends needless shouting.
It's gr8 for ending deb8s and guessing
although its spelling contradicts text messaging...

ahem

In the Java Language Specification (JLS) Second Edition, it is written:

6.6.1, Determining Accessibility, page 104

A member (class, interface, field, or method) of a reference (class, interface,
or array) type or a constructor of a class type is accessible only if the type is
accessible and the member or constructor is declared to permit access:

[...]

Otherwise, if the member or constructor is declared protected, then access
is permitted only when one of the following is true:

•Access to the member or constructor occurs from within the package
containing the class in which the protected member or constructor is
declared.

•Access is correct as described in 6.6.2

6.6.2, Details on protected Access

A protected member or constructor of an object may be accessed from outside
the package in which it is declared only by code that is responsible for the implementation
of that object.

6.6.2.1, Access to a protected Member
Let C be the class in which a protected member m is declared. Access is permitted
only within the body of a subclass S of C. In addition, if Id denotes an
instance field or instance method, then:
•If the access is by a qualified name Q.Id, where Q is an ExpressionName,
then the access is permitted if and only if the type of the expression Q is S or a
subclass of S.
•If the access is by a field access expression E.Id, where E is a Primary
expression, or by a method invocation expression E.Id(. . .), where E is a
Primary expression, then the access is permitted if and only if the type of E is
S or a subclass of S.

 

Posts:183
Registered: 5/28/04
Re: Sample java program  
Aug 5, 2004 4:58 AM (reply 11 of 12)



 
In one of the mock exam it is said that since u r not
providing the implementation of class A in class B
which is a subclass of class A thats why you cannot
access the protected variable i directly..

I think that is a correct explanation. Note that this line will compile in B, since it has acess to the superclass A.
this.i =  this.i * 2;


-Scott
 

Posts:37,103
Registered: 3/30/99
Re: Sample java program  
Aug 5, 2004 7:27 AM (reply 12 of 12)



 
Hi all
According to Khalid Mughal for Sun Certifed Java
Programmer
it is said that not only protected members are
accessible to all classes within a package BUT also to
all subclass of that class irrespective whether the
subclass is in the same package or somewhere
else........
I have extended the class A ....
Thats where I am confused.....
In one of the mock exam it is said that since u r not
providing the implementation of class A in class B
which is a subclass of class A thats why you cannot
access the protected variable i directly..
I didnt understood the above explaniation which they
were trying to explain....
Please help !!
Waiting for your reply..

I think it's because of how you tried to access it. I don't think you can access it by
A a = ...;a.i... 
I think a B can only access an i that is its own member variable, through an explicit or impied "this".
 
This topic has 12 replies on 1 page.