Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q on overriding

 
srini v
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:


When cut and past the above code, it prints "C.m" as expected.
When I change the code

to

I get "B.m".
Please clarify?
 
H Gokulam
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
'default access' means accessable only in the same package.
'protected access' means accessable to all classes derived from this class evenif the class is in a different package.
You changed protected access to default access. ie: by doing that you made that method (void m()) not accessable outside the package.
So the method protected void m(){ System.out.println("C.m"); in class C is not overriding the method in class B, it is just hiding/shadowing the method in class B.
Hope it is clear now.
 
Lee Ming
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi srinivasan,
You can find more information about method access controllhere
 
srini v
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hari, Lee thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic