This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Subclass in a different package

 
Amol Keskar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the following file in the package "newpackage"

package newpackage;
public class Animal
{
protected int i, j;
protected static int x = 60;
protected static int returnX()
{
return x;
}
protected Animal()
{
i = j = 10;
System.out.println("Animal Constructor called");
}
protected void Eyes(int k)
{
i = k;
j = k+1;
}

public static void main(String[] ars)
{
}
}
Then I have the following child class (it extends the above Animal class) defined in different package

import newpackage.*;
public class AnimalChild extends Animal
{
public void test()
{
AnimalChild a = new AnimalChild();
System.out.println(a.i);
}
public static void main(String args[])
{
Animal b = new Animal();
AnimalChild c = new AnimalChild();
c.test();

b.i = b.j = 25;
System.out.println(" " + b.i + " " + b.j);
b.Eyes(4);
System.out.println(" " + b.i + " " + b.j);
}
}
I cannot get to compile AnimalChild class. My classpath is set properly to take care of different packages. But the compiler complains that the variables, methods in Animal class have protected access modifier. As per my understanding, a child class can access protected variables and methods of its parent class EVEN THOUGH it is in a different package. But I am not able to compile the above class?
Thanks for your help!
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amol,
There's a slight hitch to protected access, the accessing object has to implement the superclass object

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.

If you substitute the 'c' object everywhere you've used the 'b' object in AnimalChild the code will correctly compile and run ie you'll be able to access all the protected members defined in Animal through an AnimalChild object. Merely creating an Animal object within the AnimalChild class is not enough. ie the protected members are available to any subclass, in any package only through a subclass object.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited May 27, 2001).]
 
Amol Keskar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jane,
Thanks a lot! I did not know about this hitch in protected access.
I guess, I am thinking is it extremely essential to read through java language specification as a part of your study for certification? I dont think without reading JLS I would have known about this problem?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic