Two Laptop Bag*
The moose likes Java in General and the fly likes java packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java packages" Watch "java packages" New topic
Author

java packages

Skm Liyanage
Greenhorn

Joined: Sep 09, 2012
Posts: 19

package p1;
public class A{
public A(){
myMethod();
}
void myMethod(){
System.out.println("p1.A.myMethod");
}
}
//--------------------------------------------
package p2;
public class B extends A{
public void myMethod(){
System.out.println("p2.B.myMethod");
}
}

//--------------------------------------------
package p3;
public class Demo{
public static void main(String args[]){
new p2.B();
}
}

above program outputs "p1.A.MyMethod".
please explain how to come this output?


Come, we all correct our wrongs.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Did you try running it to see its output?


The quieter you are, the more you are able to hear.
Skm Liyanage
Greenhorn

Joined: Sep 09, 2012
Posts: 19

yes, it outputs "p1.A.myMethod" but it bust be "p2.B.myMethod". where is the wrong?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

A class can only override what it can see. Since the method is package private, and the extending class is in a different package, class B does not override the method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Also, your class B does not have a constructor, so default constructor of the parent class A is invoked before everything else.
Skm Liyanage
Greenhorn

Joined: Sep 09, 2012
Posts: 19

why isn't myMethod() which is in class B, overriding method?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Because it's not public in class A, so it's not possible to override it in class B (only public methods are accessible from other package).
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

overriding occurs when there is inheritance. now in your case, class A has a myMethod() which has a default(also called package private) scope. you are aware of various access modifiers like public, protected, private . there is one more scope called default scope and it comes when YOU DON'T specify any access modifier.

now a default scope means , that member is only visible to classes in the same package.
Protected scope means, member is visible to classes in same package and also to subclasses(through inheritance)
private members are only visible to other members of the same class.
public is visible to everyone.

now your subclass B extends A, but it does not know that class A defines myMethod. the method is invisible for class B . by coincidence B method specifies a method with the same name as in its superclass. so there is no overriding occuring. if you change the access specifier of method in class A to either protected or public then overriding will occur and will print p2.b.myMethod.


P.S. your code won't compile. in class B extends A , you are not importing class A and neither you are using fully qualified name.
Skm Liyanage
Greenhorn

Joined: Sep 09, 2012
Posts: 19

now i can understand.thanks a ton all of you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java packages