Results 1 to 8 of 8

Thread: Overriding

  1. #1
    prasanna1157 is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  3. #3
    prasanna1157 is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default 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

  4. #4
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    But, what would happen if it were allowed?

    Didn't masijade said
    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.
    -Regards

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by prasanna1157 View Post
    Could you illustrate?
    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.

  6. #6
    prasanna1157 is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default 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

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default

    Quote Originally Posted by prasanna1157 View Post
    Since I wasn't clear initially even after getting a reply there, I thought I needed another example. That's why I posted.
    You said the same thing on the Sun/Oracle forums.

    db

Similar Threads

  1. Overriding question
    By alacn in forum New To Java
    Replies: 1
    Last Post: 08-16-2010, 11:04 AM
  2. overriding toString method
    By matin1234 in forum New To Java
    Replies: 3
    Last Post: 06-01-2010, 04:35 AM
  3. Overriding
    By renuka_renukut in forum Advanced Java
    Replies: 3
    Last Post: 05-21-2010, 08:45 AM
  4. Overriding equals method
    By sky in forum New To Java
    Replies: 7
    Last Post: 03-12-2010, 03:39 PM
  5. Overriding Methods
    By AndrewM16921 in forum New To Java
    Replies: 2
    Last Post: 09-23-2009, 06:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •