简体   繁体   中英

Difference between two Java function declarations

I was reading Hadoop documentation when I came across two function declarations returning a reference to an abstract class:

public FSDataInputStream open(Path f) throws IOException
public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException

Apart from differences in the parameters, why do these two functions have different return type, one explicitly declared abstract while other not?

Thanks.

Abstract methods are declared but have no implementation. Sub classes are forced to implement them and cannot inherit them as they are not implemented in the super class.

To answer the original question too, abstract is not part of the return type, it's a modifier of the method itself, just like public . The return types of the two methods are identical.

The class where public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException is defined is itself abstract. This means that the class has just provided a declaration of the method and not the implementation.

The other one can be either of the following:

  • If decared in an interface, it would suggest that the method is again an abstract declaration.

  • Or else, it might be a part of a concrete class, suggesting that it has an implementation over there.

The abstract method must be implemented. It's not clear exactly why in this case without knowing what the subclasses are, but if you were to subclass whatever class/interface this is in, you would have to implement the second method, while you could use the first right away.

Accoding documentation :

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

 abstract void moveTo(double deltaX, double deltaY); If a class includes abstract methods, the class itself must be declared abstract, as in: public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); } 

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException

This does not mean that we are returning an abstract class, but that we are declaring a method, but we do not gives its implementation yet, this they keyword abstract .
This is done in abstract class. So another class will extend the abstract class and give its implementation.

public FSDataInputStream open(Path f, int bufferSize) throws IOException

Tells thats we are now giving the implementation of the abstract method.

1. public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException , Means this method is an abstract method , so its just an Declaration not an Implementation. So this method can be either a method in an Interface or in an Abstract Class. And so this method must be implemented by the very first Concrete subclass that implements the Interface or extends the class which contains this method.

2. public FSDataInputStream open(Path f) throws IOException , this method if declared inside the interface, then its an abstract method , else a Non-Abstract method within an Abstract or Non-Abstract class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM