Results 1 to 8 of 8
Thread: Overriding
- 09-07-2010, 06:00 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Overriding
Hello All,
Why is that a subclass method that is overriding a base class method can't be more restrictive?
That is, why can't the access specifier be protected/private when the base class method is public?
Please explain with examples.
What is the primary reason behind disallowing this?
Thanks,
Prasanna
- 09-07-2010, 06:59 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Because the developers of the language wanted it that way. Also, the developer of a class that creates a method that is to be public wants it to remain public even in subclasses. Also, extending a class means the sub class is, also, a valid instance of the super type, but if one of the methods is now more restricted than it was for the super type, that subclass would no longer be a valid instance of that supertype.
Now, I hope this answers your homework question well enough. An example you can come up with for yourself.
- 09-07-2010, 07:06 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Method Overriding
Thank You.
But, what would happen if it were allowed?
Could you illustrate?
This is not a homework question!! It is something that I was just pondering. That's why I asked for an example, as I couldn't think of what would happen when something like that is allowed!!
Thanks,
Prasanna
- 09-07-2010, 07:12 AM #4
But, what would happen if it were allowed?
Didn't masijade said
-Regardsbut if one of the methods is now more restricted than it was for the super type, that subclass would no longer be a valid instance of that supertype.
- 09-07-2010, 07:14 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
No. Besides I see you've had this question, and more, answered in your cross-post on Sun and that cross-post contains a reference to another cross-post, as well.
- 09-07-2010, 07:19 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Method Overriding
Oh, that's fine.
Yes, I did post there as well.
Since I wasn't clear initially even after getting a reply there, I thought I needed another example. That's why I posted.
I did it just because I did not understand it at the time of posting. I don't know why that would be a problem.
Thanks,
Prasanna
- 09-07-2010, 07:20 AM #7
The other cross post
Method Overriding (Java in General forum at JavaRanch)
db
- 09-07-2010, 07:47 AM #8
Similar Threads
-
Overriding question
By alacn in forum New To JavaReplies: 1Last Post: 08-16-2010, 11:04 AM -
overriding toString method
By matin1234 in forum New To JavaReplies: 3Last Post: 06-01-2010, 04:35 AM -
Overriding
By renuka_renukut in forum Advanced JavaReplies: 3Last Post: 05-21-2010, 08:45 AM -
Overriding equals method
By sky in forum New To JavaReplies: 7Last Post: 03-12-2010, 03:39 PM -
Overriding Methods
By AndrewM16921 in forum New To JavaReplies: 2Last Post: 09-23-2009, 06:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks