• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

java packages

 
Greenhorn
Posts: 23
Java ME TypeScript Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try running it to see its output?
 
Sameera Liyanage
Greenhorn
Posts: 23
Java ME TypeScript Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, it outputs "p1.A.myMethod" but it bust be "p2.B.myMethod". where is the wrong?
 
Sheriff
Posts: 22696
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sameera Liyanage
Greenhorn
Posts: 23
Java ME TypeScript Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why isn't myMethod() which is in class B, overriding method?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sameera Liyanage
Greenhorn
Posts: 23
Java ME TypeScript Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now i can understand.thanks a ton all of you
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic