Question

Consider the following code.

A.java, main package:

package pkgs.main;

public class A {
    protected int x;
}

B.java, test package:

package pkgs.test;
import pkgs.main.A;

public class B extends A {
}

When the javac compiler is run, does it do something roughly like this:

A.java, main package:

package pkgs.main;

public class A {
    protected int x;
}

B.java, test package:

package pkgs.test;
import pkgs.main.A;

public class B extends A {  
    // Some "flag/code" that says:
    // Must be aware that the following new and seperate copy of x 
    // has originated from class A, which is inside a package called main.
    protected int x;
}

The reason why I ask is that I'm trying to understand if it's essential that a subclass must be aware of all of the "properties/attributes" of its inherited members, in order to operate. If that is true, then would this kind of programming inheritance differ slightly from inheritance found in the "real world"? For example, I inherit various properties from my parents, but I can be completely ignorant of all of this information. (Say if my parents were to pass away, I could continue to operate.) Whereas, I wonder if Java's inheritance isn't independent in this sense, if a child subclass must always be aware of where it got its inherited members from, in order to correctly operate.

The thing is, I get confused when reading about inheritance in programming books. They provide examples involving real world objects such animals and cats and dogs etc, but based on the paragraph above, I'm wondering if there is a subtle "mechanism difference" between Java inheritance, and inheritance examples given in Java books that involve these types of real world objects.

The problem that I'm having with Java is that I don't understand why I'm allowed to do the following. The code below is some simple "calling code" in a "Main class" involving the two classes (A and B) already defined above.

Main.java, main package:

package pkgs.main;
import pkgs.test.B;

// Just some method inside the "main class"
void method() {
    B b = new B();
    b.x ++;  //   <--- why is this allowed?
}

This "calling code" is inside a package called main, and class B is defined inside a different package called test. This calling code is not extending class B, so why does Java allow access to B's protected member x?

Is the explanation because the protected member x was originally declared inside class A, which is in the same package as the calling code? If true, would that roughly match my earlier thoughts, where I said this about class B:

public class B extends A {  
    // Some "flag/code" that says:
    // Must be aware that the following new and seperate copy of x 
    // has originated from class A, which is inside a package called main.
    protected int x;
}
Was it helpful?

Solution 2

// Some "flag/code" that says:
// Must be aware that the following new and seperate copy of x 
// has originated from class A, which is inside a package called main.
protected int x;

No. There isn't a 'new and separate x'. It is the 'x' inherited from class A. It's the same thing. That could explain all your confusion.

if a child class must be aware where it got its inherited members from

Of course.

Is the explanation because the protected member was originally declared inside class A?

Yes.

OTHER TIPS

What does the Java compiler do when it sees the keyword extends?

Nothing in your case.

This calling code is not extending class B, so why does Java allow access to B's protected member x?

It's about accesibility. According to the Java Language Specification (see JLS7: http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf)

6.6.1 Determining Accessibility

...

Otherwise, if the member or constructor is declared protected, then access is permitted only when one of the following is true:

  • Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.

  • Access is correct as described in §6.6.2.

...

you can access the protected field 'x' from the same package. But if you declare a new field 'x' in class 'B' (which hides 'A.x') then you can't access 'x' (in the form b.x++;) from the package 'main'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top