• Post Reply Bookmark Topic Watch Topic
  • New Topic

Protected or public  RSS feed

 
Arathi Raj
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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
 
Tony William
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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".
 
Sathya Srinivasan
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • 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.
 
Vinayagam Kulandaivel
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Techie,

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

Vinay
 
Emilia Ipate
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Sankar Ramakrishnan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!