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

Abstract question

 
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, good day, i have a question in abstract class modifier

abstract class :


extend class :


as sample code shown, my problem is why if i ignore to put "public" into OtherPackAbstractChild 's displayParent() method will give me "attempt assign weaker access privilege" error ? as i make the displayParent() method in TestAbstractParent class as public ..it suppose allow me to access from anyway right ? thank you very much for reading ..
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a method in a subclass overrides a method in the parent class, the access specified by the subclass method declaration must be no less than the access of the superclass method. In this case, the superclass method has public access and you tried to give the overriding subclass method default (package) access.

The reason for this rule is that a client of the superclass is relying on the public interface (contract) of the superclass, including the headers of the non-private methods. If a client uses a superclass type reference to access an object of the subclass type, it should be able to treat that object, including the object's methods, as if it were accessing an object of the superclass type. After all, the subclass object is-a superclass object.

In this case, a client trying to call the method on a subclass object using a superclass reference, relying on public access, would be out of luck.

This is the reason for the rule.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks mike for explanation
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic