• 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

Package Problem...

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel that you're answering you're own question
 
Ashutosh Limaye
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the disp() of SubClass is called...


But only the super class's disp() is visible.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,,,
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic