• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Method parameter

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() );
}
}
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() );
}
}
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification Kapil.

:-)

JerryB
 
Vishal Gupta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jerry,
BTW I tested it in Eclipse too and it works.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic