aspose file tools*
The moose likes Java in General and the fly likes Cannot reduce the visibility of the inherited method from super class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cannot reduce the visibility of the inherited method from super class" Watch "Cannot reduce the visibility of the inherited method from super class" New topic
Author

Cannot reduce the visibility of the inherited method from super class

surya ojha
Greenhorn

Joined: Mar 03, 2011
Posts: 2
Why cannot reduce the visibility of the inherited method from super class ?

what is the specific reason for above problem.

I am unable to absorb it. Kindly explain.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4540
    
    5

Simple answer: because the Java Language Specification says so.
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.3
(Scroll down to the end of the section)
The access modifier (ยง6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs. In more detail:

  • If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.
  • If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; otherwise, a compile-time error occurs.
  • If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.


  • Oh, and welcome to the Ranch!


    luck, db
    There are no new questions, but there may be new answers.
    Mohamed Sanaulla
    Saloon Keeper

    Joined: Sep 08, 2007
    Posts: 3068
        
      33

    Hi Surya, Welcome to JavaRanch.

    Its how the language was designed. There are few things in the language which the creators of it would know. But a may-be reason: The Method is owned by the Super class and its not fair if the sub class suppresses it/reduces its visibility. Also in case we use a Super Class reference and try to invoke the overridden version of the method in the Sub class by creating a Sub class instance and assigning it to a Super class reference- this wouldn't be possible as the overridden method is not accessible outside the class declaration.

    Other members might have a better answer

    Update: Darryl was quicker

    Mohamed Sanaulla | My Blog
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4385
        
        8

    To expand on what Mohamed said, consider this example:
    There's a principle in object-oriented programming called the "Liskov substitution principle" that says that whenever an instance of a class is required, an instance of any subclass can be used as well. So the code on lines 11/12 above should be valid. We should always be able to assign a B to an A variable. But what happens when we try and call myMethod? It should be OK, because myMethod is public in A, and we've got an A variable. But it's really a B object, so it can't be allowed! We've got an inconsistency. The rule is in place to prevent this.

    Note that increasing the visibility doesn't cause this problem, and so it's allowed.
    surya ojha
    Greenhorn

    Joined: Mar 03, 2011
    Posts: 2
    Matthew Brown wrote:To expand on what Mohamed said, consider this example:
    There's a principle in object-oriented programming called the "Liskov substitution principle" that says that whenever an instance of a class is required, an instance of any subclass can be used as well. So the code on lines 11/12 above should be valid. We should always be able to assign a B to an A variable. But what happens when we try and call myMethod? It should be OK, because myMethod is public in A, and we've got an A variable. But it's really a B object, so it can't be allowed! We've got an inconsistency. The rule is in place to prevent this.

    Note that increasing the visibility doesn't cause this problem, and so it's allowed.


    thanks for describing. thanks a lot. I got it. (atleast one reason).
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38756
        
      23
    Another way to describe it is that the subclass method is a refinement of the superclass method. A refinement means that in all instances where the refined operation is feasible, the refining operation is feasible. ∀x·fis(superclass(x)) ⇒ fis(subclass(x))

    This is incorporated in the Liskov Substitution Principle.

    That means, if you can do it in the superclass, you must do it in the subclass too, so you mustn't change the subclass method from public to private, or throw new Exceptions or anything. The compiler enforces that as far as possible, but the compiler can't check unchecked Exceptions.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Cannot reduce the visibility of the inherited method from super class