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

Overriding methods with different access modifiers and exceptions

 
Ranch Hand
Posts: 472
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 472
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Let nothing stop you! Not even this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic