• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java packages

 
Skm Liyanage
Greenhorn
Posts: 23
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try running it to see its output?
 
Skm Liyanage
Greenhorn
Posts: 23
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it outputs "p1.A.myMethod" but it bust be "p2.B.myMethod". where is the wrong?
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why isn't myMethod() which is in class B, overriding method?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i can understand.thanks a ton all of you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic