Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding methods with different access modifiers and exceptions

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when i work with access modifier i use this code. and i have a question about polymorphism:
bA.prMethWE(); - i can`t use it but i cant change it type because it will fail overriding rules
bA.defMethWE(); - i can`t use it because it has default access modifier, but i can change it signature in derived class
i assume that if you use reference type of basic class you can`t use overridden method if in basic class it has protected and derived class that override this method place in separate package. (but it still must follow overriding rules)
and about default method from base class it will not considered like overridden if subclass place in separate package.
is my assumption right?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any overridden method MUST meet (adhere to) all the overriding rules. One important rule to remember: you can only override a method if this method is inherited (by the subclass). So the visibility/accessibility of a default method is limited to the package of the class which defines this method. So a subclass of this class, defined in another package, will NOT inherit this method, so the overriding rules don't apply (and you can change e.g. return type and/or checked exceptions in throws-clause)

A protected method can be used/accessed by a subclass in another package, but ONLY through inheritance (NOT using the dot operator on a reference variable).
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


A protected method can be used/accessed by a subclass in another package, but ONLY through inheritance (NOT using the dot operator on a reference variable).

i assume that you mean NOT using the dot operator on a reference variable of base class (one class up on hierarchy) (if inside subclass)
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergej Smoljanov wrote:i assume that you mean NOT using the dot operator on a reference variable of base class (one class up on hierarchy) (if inside subclass)

The protected access modifier is the most tricky one. When a class inherits a protected method from its parent class, the method is treated as a "private" method, but a subclass of the subclass can still inherit this method too. So that means if you are in (a method of) the subclass, you can use a subclass reference variable to invoke this protected method. But if you use a superclass reference variable to invoke this protected method, you'll get a compiler error. If you are in (a method of) the subsubclass (the subclass of the subclass) you can use a subsubclass reference variable to invoke this protected method. But if you use a reference variable of any of its superclasses to invoke this protected method, you'll get a compiler error.

This example illustrates all these possible scenarios:


Hope it helps!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic