aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method parameter" Watch "Method parameter" New topic
Author

Method parameter

Malini Santhanam
Greenhorn

Joined: Jul 20, 2004
Posts: 4
Can anybody let me know why the following code will not compile? This is one of the question from JQ plus test series.

//in file A.java
package p1;
public class A
{
protected int i = 10;
public int getI() { return i; }
}

//in file B.java
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(a);
System.out.println( a.getI() );
}
}

Thanks,
Malini
Vishal Gupta
Greenhorn

Joined: Jul 15, 2004
Posts: 21
That's because you are trying to access the protected member of Class A through instantiation.
In the following code:
You can access the protected member i through inheritance.

//in file B.java
//package p2;
import p1.A;
public class B extends p1.A
{
int j=i;
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(a);
System.out.println( a.getI() );
}
}


SCJP 1.4, SCWCD 1.4
George Koshy
Greenhorn

Joined: Jul 21, 2004
Posts: 8
Hi ,

If you change the code to the followng in case of class B , it will compile and run.

The below is the correct way to use a protected variable in a sub class.

(this.i will work as well) . Hope this helps ...

package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
i = i*2;
}
public static void main(String[] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println( a.getI() );
}
}
Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Good Morning Black Hawk,

I examined your response to Malini's question and duplicated it in Eclipse. Your solution, of course, worked. I do not understand why in package p2 does the specific import of class A, as in import p1.A;, solve the problem? Does not the import in Malini's code, import p1.* also import class A if needed?

Thank you in advance,

:-)

JerryB
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Hi all,

Well, Class is definitely imported into class B, but Class B is not a subclass of Class A. This is why protected variable i cannot be used in package p2.

'protected access modifier' explanation can help you understand this.

We can access the protected variable in the same package or in the subclass(even if the subclass is in another package).

Kaps
[ July 27, 2004: Message edited by: kapil munjal ]

Kapil Munjal
SCJP 1.4, SCWCD 1.4
Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Thanks for the clarification Kapil.

:-)

JerryB
Vishal Gupta
Greenhorn

Joined: Jul 15, 2004
Posts: 21
Jerry,
class B extends class A !!
So class B does inherit from class A although it is in a different package.

To answer your question,
you can use the wildcard * instead of just using
import p1.A;

Just make sure that the class files are in appropriate directories.
So,
A.class should be in a directory "A"
B.class should be in a directory "B"

Hope this answers your question..
Vishal Gupta
Greenhorn

Joined: Jul 15, 2004
Posts: 21
Jerry,
BTW I tested it in Eclipse too and it works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method parameter
 
Similar Threads
Question from JQPlus
please answer the question and explain it to me,thanks!
why compile time error???
How does this work ?
Compile or not?