wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Package Problem... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package Problem..." Watch "Package Problem..." New topic
Author

Package Problem...

Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
Hello,

Here is a Scenario...










How can the main in TestSupSub call the protected method of SubClass since it is not in the package of SubClass? The above scenario works. It can call the disp() method of the SuperClass since it is in the same package that of SuperClass....
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I feel that you're answering you're own question


[My Blog]
All roads lead to JavaRanch
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
I didn't get you?
The call in the main is polymorphic... the disp() of SubClass is called...Where SubClass and TestSupSub are not in the same package...
[ June 29, 2007: Message edited by: Ashutosh Limaye ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

the disp() of SubClass is called...

But only the super class's disp() is visible.
Biswajit Paria
Ranch Hand

Joined: Feb 02, 2003
Posts: 46
package pack1;public class TestSupSub{public static void main(String []args){SuperClass s= new SubClass();s.disp();}};



Hmmmm...I got your point...
ok..In your Test class while you are calling s.disp();, it means...your compiler is dreaming that you are calling SuperClass's method (it is called runtime polymorphism), So it does not warn you...but actually at runtime you will get subclass's method call...

Now if you do like new SubClass().disp() your compiler will warn you that it is not visible to it. as you know that protected method it not visible to other classes unless extended....
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
Hmmmm...I got your point...
ok..In your Test class while you are calling s.disp();, it means...your compiler is dreaming that you are calling SuperClass's method (it is called runtime polymorphism), So it does not warn you...but actually at runtime you will get subclass's method call...

Now if you do like new SubClass().disp() your compiler will warn you that it is not visible to it. as you know that protected method it not visible to other classes unless extended....


Excatly....
So it means that at runtime the TestSupSub class is accessing the protected method of a class to which it is not related through inheritance and is not in its package as well...
don't you think this is wierd ???
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

protected method of a class to which it is not related through inheritance

SuperClass is related to SubClass. What do you mean by not related through inheritance ? And remember that "protected" is accessible by classes in the same package, which is the case for your main class.
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
Point 1: TestSupSub and SuperClass are in pack1.

Point 2: SubClass is in pack2.

Point 3: There is no relation between TestSupSub and SubClass (ie. Inheritance)

Point 4: I'am executing the main of TestSupSub.

Point 5: If I write new SubClass().disp() in the main will it work...I guess not.(ERROR: TestSupSub.java:9: disp() is not public in pack2.SubClass; cannot be accessed from outside package
new SubClass().disp()

So how should the the call s.disp() work?... s is pointing to SubClass object at runtime,,,
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Originally posted by Ashutosh Limaye:
Point 3: There is no relation between TestSupSub and SubClass (ie. Inheritance)


That's right, but there is ofcourse an inheritance relation between SuperClass and SubClass.

Because TestSupSub is in the same package as SuperClass, it has access to the disp() method in SuperClass. Note that this check is done statically (at compile time). The compiler looks at the type of variable 's' (which is SuperClass) and that determines if disp() is visible or not.

In your program the variable s points to an instance of SubClass. Polymorphism works at runtime (dynamically); when you run the program, it will at that moment find out that the object that 's' refers to is actually an instance of SubClass, and then it will call the overridden disp() method in SubClass.

The access specifier check is not done at runtime.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jay Bash
Greenhorn

Joined: Jun 29, 2007
Posts: 5
Hi

What I think is given following code

SuperClass s= new SubClass();
In TestXXXX.java won't work. It should give you the compile time error.

It should give you compile time error as SubClass is not visible in TestXXX class.

if you add the import statment then actual poloymorphism will work and call the SOP of sub class will be printed.
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
I guess what came into work is what is called dynamic method dispatch.
that means that the call is resolved at run-time.actually superclass reference variable can access subclass methods which are common to both.try calling another protected method of sub-class which is not present in superclass and you will get an error.
This property is call runtime polymorphism.
hope this solved your problem


Jitendra
SCJP1.5
SCWCD1.5
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Package Problem...