• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

bet u cant ans this

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
take this
// Parent.java
package p;
public class Parent { final void a() {} }
// Child.java
package c;
import p.*;
public class Child extends Parent {
void a() {}
public static void main ( String [] args ) {
Parent p = new Child() ;
p.a() ; // which method parent or child
}
i.e. class parent in package p has a method a with package accessiblity and this method is not accessible in packge c
now what will happen in the main method
will polymophism work : will it give a compile errror
what will happen if i put the main method in class Parent and run it here as a inherited method
what will happen if i put the main method in parent and run parent.class after importing package c there
i hope u get my point
waiting for a good discussion here
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First point here, I think your code gives a compiler error since the method "a" in class parent is declared as final and thus can't be overriden in class Child. If you remove the modifier final in the signature of method "a" in class Parent, the code will still not compile since as you pointed out the method "a" has package accessibility. In order for the code to compile correctly you should at least give method "a" in class Parent protected accessibility !
Try !

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Mateen Nasir
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final and default is the whole point in question.
the cause of all the debate
that since the method of parent is not visible in child the child method should not override. infact it should come as a new method in child not a overridden one .
I am not being able to try this because problem is i have tried this on differnt tools and dos window and results are different.may be a fault of my settings. so i want to discuss this on merit.
waiting !
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


that since the method of parent is not visible in child the child method should not override. infact it should come as a new method in child not a overridden one .


NO, that's not true... Look, this is what I get by compiling your code with JDK 1.4 beta 2 on Linux

I think this is clear enough! If you do not declare correctly the method "a" in class Parent you won't be able to override it. If the purpose is not to override it, then just choose appropriate method names that will not conflict together...
Anyone a better explanation ?

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Valentin. The basic problem is that
a() in Parent is a method with default (package) accessibility.
Since Child is in a different package than Parent, it will fail
compilation right at line p.a();
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All.
I agree that method a() in Parent class has default access that is only in package p.But after importing this package(p) in package c we can access method a() of package a().
Well, the method a() cannot be overriden in child class.Since it is declare as final.
Regards,
Hassan.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Val do you know if we can extract a conclusion from this?
Like, the compiler will always produce an error relative to the access modifiers before any invocation error else. (I guess this sentence doesn't sound pretty much good English)
For instance the compiler will complay that we can't access a "friendly" abstract class from another package before trying to create an instance of it.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess what is discussed in this thread is pretty similar as what happens here.
http://www.javaranch.com/ubb/Forum35/HTML/000381.html
can you guys have a look and clarify me? I even don't know how to test the question(I am using JBuilder), tried several times unsuccessfully.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony,
it is similar, yes but your guess in that thread is wrong, check it out, I wrote some explanations

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Mateen Nasir
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference b/w inaccessible and invisible ?
( is it true for private too i.e. being invisible and inaccessible)
secondly consider what will happen if i move this main method to parent class and import the package c containing the child class there
will the polymorphism work then !!
waiting for comments !
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mateen,


secondly consider what will happen if i move this main method to parent class and import the package c containing the child class there will the polymorphism work then !!


Interesting! Now you have these situation:
package p depends on package c and
package c depends on package p
How can you compile the code in the first place?
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mateen,
a package is always accessible, thus it is legal to TRY to access some package members even if we know that it won't succeed at compilation. Accessibility of package members is ruled by the access modifiers (public, protected, "default" and private). As you certainly know, "default" members are accessible only for members of the same package and not outside.
Private members are not inherited and thus are not visible and not accessible to other classes.
Concerning your moving the main into the Parent class:
you can move the main method in the Parent class but then you have to :
- import the Child class inside the Parent class
- declare your method "a" to be at least protected so that overriding works
When you run the Parent class the method "a" of the Child class gets called as expected...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nain,
there is no problem in Java concerning circular dependencies during compilation, you just have to set the CLASSPATH accordingly and everything works juts fine.
you have two packages (say directories) A and B, A depends on B and B depends on A. Run the following command (Assume you are just above A and B):
java -classpath A:B <some-classes.java> (on Linux and Unix)
java -classpath A;B <some-classes.java> (on Windows)
The only circularity issue that is prohibited in Java is circular inheritance (A is_a B AND B is_a A).
Anyone correct me if I'm wrong

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Val, Thanks. I tried and it worked.
 
Mateen Nasir
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
Mateen,
Concerning your moving the main into the Parent class:
you can move the main method in the Parent class but then you have to :
- import the Child class inside the Parent class
- declare your method "a" to be at least protected so that overriding works
When you run the Parent class the method "a" of the Child class gets called as expected...
HIH


once i move the main to parent class i guess the need to declare
protected is gone ( if i am not aiming at polymorphism of course)
see since the main method is in the same package as parent
( ofcourse since the same class ) paerent's method with default
access are visible to it
now what i guess will happen is : the child class will define a
method which will not be an overided method but a new method
now when i cast the child object to a parent reference and call
the parent method the compiler can see that the parent has this
method and will not give an error!
and when the program is run the child's method will b run
AgreeD ??
waiting for a comment !
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you put main in the Parent class you still have to import c.Child
Then when you try to compile the compiler still complains about method a (in class child) being not correctly overridden which makes sense since Child is a subclass of Parent. method a in Parent is still visible in Child but not accessible !!!
The cast you are trying to do in Parent won't help since the runtime type of p won't change because you are casting it...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited October 03, 2001).]
 
Mateen Nasir
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
If you put main in the Parent class you still have to import c.Child
Then when you try to compile the compiler still complains about method a (in class child) being not correctly overridden which makes sense since Child is a subclass of Parent. method a in Parent is still visible in Child but not accessible !!!
The cast you are trying to do in Parent won't help since the runtime type of p won't change because you are casting it...
HIH



what is the difference of being accessible and visible.
are't they the same
remember that a method that has default visiblity acts as a
private if seen from outside the package
and i dont guess child class should have any problems in overiding aa private method
Secondly , the presence of main in the parent's package should
allow the call to child method to compile ; that was what i meant
humm ...so waht do u think
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try to understand the code

compile it and you'll see that it doesn't compile, actually it compiles but generates a warning

Accessible and visible is not the same. Since Child extends from Parent the compiler sees that there are two method "a", one in Parent and one in Child but the one in Child does not override correctly the one in Parent, which means "a" in Parent is visible in the Child (due to inheritance) class but not accessible (dur to the modifier "default")...
Let me know what you think...
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited October 03, 2001).]
 
Mateen Nasir
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well well firstly warning is not an error : i guess guess a recommendation because u will still get a class file which will run perfectly fine
just as u said i tried the two below and the output
wow !
first the parent file Parent.java
package p;
import c.*;
public class Parent{
public static void main(String [] arg){
System.out.println( " Allah help me" );
Child ch = new Child () ;
Parent pa = ch ;
pa.zzz() ;
ch.zzz() ; }
final void zzz() {
System.out.println( " from parent" );
}
}
now the child file Child.java
package c;
import p.*;
public class Child extends p.Parent{
public void zzz ( ) {
System.out.println( " from child" );
}
public static void main(String [] arg){
System.out.println( " Allah help me" );
}
}

now the output
Allah help me
from parent
from child
I HOPE U GOT WHAT HAPPEEND
let me just hint
when u compile the child
yes the compiler found that u might be trying to override a
final method which since not being visible in the child class is
perfectly ok. so it did not give u any error. but warned that
if u had desired overriding that has not ocured
now when the compiler is compiling the parent it simply gives no warining why? that is because it finds that the parent has a method zzz and a child has a method zzz so the compile time check is ok
NOTE : move the main to the child and it does not compile
The real stuff comes at runtime
when we cast the child ch to parent reference and call zzz
method the parent sees that this is a final method of the refence and hence cannot be overridden ans therefore runs it there: I AM NOT SURE BUT MAYBE FINAL METHODS WORK LIKE STAIC
ONES IN THIS CASE BECAUSE U DO NOT NEED TO SEE THE RUNTIME OBJECT
so now while the child refernve runs the child method
the parent reference runs its method
although they both point to the same object( aliases) !!!
I hope i am making it clear
but its stil open for discussion
LASTLY before i leave the difference is unclear still : from this prog what i see i that anything visible and accesible and vice versa

[This message has been edited by Mateen Nasir (edited October 03, 2001).]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great explanation Mateen Thanks !!

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see, I can't see:
JLS 6.3.1
"A declaration d is said to be visible at point p in a program if the scope of d includes p, and d is not shadowed by any other declaration at p. When the program point we are discussing is clear from context, we will often simply say that a declaration is visible."
and
"Some declarations may be shadowed in part of their scope by another declaration of the same name, in which case a simple name cannot be used to refer to the declared entity."
the last
"The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name (provided it is visible"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic