Re: FINAL keyword
kaja_love160@yahoo.com wrote:
1)
If method ( call it U ) in parent class is made private, then methods
of child class can???t access U directly, but they still inherit this
private method U.
No.
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8>
A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden (??8.4.8.1) nor hidden (??8.4.8.2) by a declaration in the class.
Also, methods don't inherit methods anyway, classes do.
Is it similar situation if parent method ( call it U1 ) is declared
final ??? Meaning child methods can???t access U1 directly, but they still
inherit it?
No. "final" (not "FINAL" as in your subject, because that's not a keyword)
does not mean "child methods can???t access [it] directly", and methods don't
inherit methods anyway. Classes inherit methods.
"final" does not control accessibility, only overridability.
2)
Subclass can override parent???s class method by creating method with
same name and same parameter list --> this is not true if parent
method is declared private
Correct so far.
So if compiler identifies a method by its name and its parameter list,
then why do we get compile-time error if ???hey()??? in B is private
( compiler informing us that ???hey()??? can???t be overriden ):
What is the error message? What is the actual code? Provide an SSCCE!
I cannot get any kind of error message from an attempt to "override" a private
method, final or not. Perhaps because it's not illegal?
class A{
private final void hey(){???}
}
This code doesn't compile.
class B extends A{
private void hey(){???}
}
This code doesn't compile.
, while compiler doesn't complain when ???hey()??? in class B is made
public:
class A{
private final void hey(){???}
}
class B extends A{
public void hey(){???}
}
? That would suggest that when it comes to overriding method, java
doesn???t only distinguish between methods by their name and parameter
list, but also by their access modifier.
Perhaps, if your facts were correct. My SSCCE didn't give anything like the
error you hinted about.
Compiling 1 source file to projects/testit/build/classes
compile:
run-single:
Parent.privFinal()
Parent.priv()
BUILD SUCCESSFUL (total time: 3 seconds)
<sscce comment="this code compiles and runs without compiler error"
source="testit/Child.java" >
/** Child.java
*/
package testit;
class Parent
{
private final void privFinal()
{
System.out.println("Parent.privFinal()");
}
private void priv()
{
System.out.println("Parent.priv()");
}
public final void invoke()
{
privFinal();
priv();
}
}
/** Child .
// warning: "Exporting non-public type through public API" from IDE.
*/
public class Child extends Parent
{
private void privFinal()
{
System.out.println("Child.privFinal()");
}
private void priv()
{
System.out.println("Child.priv()");
}
public static void main( String [] args )
{
Parent child = new Child();
child.invoke();
}
}
</sscce>
It compiles, it runs, it makes output.
--
Lew