• 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Whats wrong with the protected method?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why does a call to sub.protectedmethod() gives an error saying 'protectedmethod() not visible' ? Is not class MySubclass inheriting protectedmethod() from MyObject ?

The only way I get to compile this is by redefining protectedmethod() in MySubclass.



Now isn't that something that inheritence should do - bring a copy of protected methods (protectedmethod()) down to the Subclass? Why do I have to redefine it in the Subclass for it to be invoked?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


sub.protectedmethod(); // Why does this give error?


Because the class that this call is defined in is not either:
a) in the same package
b) a subtype
...of the type that declares the method.
 
Jaspreet Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in that case, if I make Test extend MyObject, it should enable calling sub.protectedmethod(); from MySubclass?

 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. Only code in SubClass, or a subclass of SubClass, can call that protected method on a SubClass object. Extending MyObject is not enough. That's just how it works.

If you want to make the method available to other random code, then yes, you have to override it to make it a public method, as in your first example.
 
Jaspreet Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
Nope. Only code in SubClass, or a subclass of SubClass, can call that protected method on a SubClass object. Extending MyObject is not enough. That's just how it works.

If you want to make the method available to other random code, then yes, you have to override it to make it a public method, as in your first example.



Ok, I'll live by that rule now. But I don't really understand the reasoning behind it.

Consider Object. It's clone() is protected. Now it is not available to any class in the Java World unless it redefines this method. While I don't know what's the spirit behind such an implementation, it definitely is not making use of code reuse, you see.
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic