• 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

Protected or public

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joshua.

A method is defined in superclass as public method. Can we use that method in subclass as protected method. IF yes then what is the use of doing so?

Thanks
arathi
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you can do this.

You cannot reduce the visibility of the inherited method from the super class.

correct me if I am wrong.
 
Arathi Raj
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What I was thinking was right. we cannot make public as protected. But I saw in some mock exam. If anybody else has any idea. Let us know

Thanks
 
Tony William
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arathi,

you mean you have seen questions in mock exam doing this?

In this is the case, that I think there should be a choice (assuming the question is a multiplce choice question) that "the code cannot compile".
 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot reduce the visibility of a method in a super-class when you override it in the sub-class. That would be a compilation error.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Techie,

You cant be able to assign the weaker privilleges to the super class method in the subclass.

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

Java specifies that methods may not be overridden to be more private.
For example, if you have a base class A and a derived class B declared like this:


the compiler will give you an error at line 6: "Cannot reduce the visibility of the inherited method from A"
 
Arathi Raj
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I have one more doubt. If a method cannot be overriden to be more private. Then is the other way round works.
That mean a protected method in super class can be defined in subclass as public method?

thanks
arathi
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. That will work .

Increasing the Visiblity is allowed. i e Protected in Base class and public in derived class
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the reason for this is Liskov's Substitution Principle: a subclass should be usable anywhere the superclass can be used. So if a client can access a method in the superclass, he should be able to access the same method in all subclasses, too.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic