• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Can anybody answer these questions?  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Questions:



1. Why interfaces methods cannot be decleared as protected?

2. Why overridden methods cannot be more restrictive than super class method (Assess specifier)?

3. Why overridden method cannot throw broader exception than superclass method exception?

4. Why it is not possible to change return type of overridden method ?

5. Why abstract classes are faster than interface?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

lAmit Joshi wrote:5. Why abstract classes are faster than interface?



It's because abstract class methods are found faster in an object at runtime than interface methods.
 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

lAmit Joshi wrote:1. Why interfaces methods cannot be decleared as protected?


That's a language design decision made by Sun. There is no technical reason for this except that it is in the Java Language Specification.

2. Why overridden methods cannot be more restrictive than super class method (Assess specifier)?

3. Why overridden method cannot throw broader exception than superclass method exception?


Check out the Liskov substitution principle.

4. Why it is not possible to change return type of overridden method ?


Again, check the Liskov substitution principle. It is possible since Java 5.0 to change the return type to a sub type of the original method. For instance, you can override clone() and specify that it returns an instance of your class or a sub class. For instance:
This does not violate the Liskov substitution principle because the following is still possible:
 
Marshal
Posts: 60806
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't use such a vague thread subject.
 
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

lAmit Joshi wrote:Questions:
why interfaces methods cannot be decleared as protected?


Interface defines a commitment. Implementing an interface means that we will provide all the functionalities (method) defined in that interface. If these methods are not public, then I think we can't assure this commitment for every class that implement that interface.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!