aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Subclass in a different package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Subclass in a different package" Watch "Subclass in a different package" New topic
Author

Subclass in a different package

Amol Keskar
Greenhorn

Joined: May 18, 2001
Posts: 23
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

Joined: Aug 30, 2000
Posts: 3141
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).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Amol Keskar
Greenhorn

Joined: May 18, 2001
Posts: 23
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?
 
wood burning stoves
 
subject: Subclass in a different package