Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

overriding abstract method

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code snippet is from JLS 2nd edition, 8.4.3.1 abstract methods :

An abstract class can override an abstract method by providing another abstract method declaration.

This can provide a place to put a documentation comment, or to declare that the set of checked exceptions (�11.2) that can be thrown by that method, when it is implemented by its subclasses, is to be more limited. For example, consider this code:

class BufferEmpty extends Exception {
BufferEmpty() { super(); }
BufferEmpty(String s) { super(s); }
}
class BufferError extends Exception {
BufferError() { super(); }
BufferError(String s) { super(s); }
}
public interface Buffer {
char get() throws BufferEmpty, BufferError; <====
}
public abstract class InfiniteBuffer implements Buffer {
abstract char get() throws BufferError; <====
}

When I had a look at the above code, I was wondering how come
char get() method in abstract class can override (or say implement)
the method with default access as the method in interface Buffer
is implicitily public. To prove my assumption, I compiled the above code
and it complains about the accessibility modifiers.

Can anyone else explain, whether I'm missing anything else?
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good question. I don't know -- maybe a small oversight?

The Third Edition perpetuates a similiar problem:



Although the example is meant to demonstrate problems when an overridding abstract method has the same signature but different return type as the overridden method, simple change "int" to "void" and then you get the "cannot reduce visibility" error.
 
raghu babu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ryan. I agree it might be a oversight. At first glance I thought it is a valid code and thought I might be missing a concept about overriding methods from Interfaces. This again proves, that compile any code before you believe what the authors are saying.
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic