File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method parameter

 
Malini Santhanam
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() );
}
}
 
George Koshy
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jerry Bustamente
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification Kapil.

:-)

JerryB
 
Vishal Gupta
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry,
BTW I tested it in Eclipse too and it works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic