• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can anybody answer these questions?  RSS feed

 
lAmit Joshi
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?
 
Sha Jar
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.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java 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:
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't use such a vague thread subject.
 
Muhammad Khojaye
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.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!