Using keyword abstract in methods in java
As per The Java Programming Language(by Arnold Gosling) (ref pg. 275) if we use abstract
explicitly before a method declaration in an interface, We can
skip the method method in the implementation class.
As per the example:
interface Sheet{
public double computeArea();
public abstract double computePerimter();
}
class Sphere implements Sheet{
// Some data members and constructors.
public double computeArea() { ... }
// No implementation of computePerimeter
}
But when i tried creating an object of the Sphere class, i showed up an error that the computePermeter method hasnt been overloaded(that should has been the case).
But as per the context, you could skip
the method if it has been explicitly declared
as abstract
.However there has to be a class implementating that method. Arent the methods in an interface implicitly abstract??开发者_StackOverflow Or am i interpreting it wrongly?? The explicit abstract
has confused me a bit.. please help..
First of all, if your class contains an abstract
method it has to be declared as abstract
. Like this:
abstract class Sphere
. Second, abstract
classes cannot be instantiated. Think of abstract
classes as not yet finished and not completely usable. An interface
can be thinked of a class which has all methods implicitly abstract
and public
. It defines behaviour not implementation.
Extract from the Java Language Specification:
Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.
Every method declaration in the body of an interface is implicitly public.
For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces.
The book must be wrong or at least have made a confusing claim.
Edit: The only thing I can think of is that you can implement an interface and keep the method abstract to add Javadoc to it.
interface Foo {
void bar();
}
abstract class FooImpl implements Foo {
/**
* I want to put documentation here without implementing the method.
*/
abstract void bar();
}
But this has no effect on the behaviour of the code.
The keyword abstract
makes no difference. The only thing the specification says is:
For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces.
A quick test shows that the keyword indeed makes no difference. These two are both valid:
abstract class A implements B { }
interface B {
public void meth();
}
abstract class A implements B { }
interface B {
public abstract void meth();
}
and these two are both invalid:
class A implements B { }
interface B {
public void meth();
}
class A implements B { }
interface B {
public abstract void meth();
}
A.java:1: A is not abstract and does not override abstract method meth() in B class A implements B { } ^ 1 error
If you implement an interface you need to implement the methods of the interface or declare them abstract, which will make the class abstract.
effectively an abstract class is a partial interface and/or all interfaces are abstract classes.
interface Sheet{
public double computeArea();
public abstract double computePerimter();
}
Is exactly the same as
interface Sheet{
public double computeArea();
public double computePerimter();
}
Abstract classes are classes that are partially implemented and cannot be instantiated because they are not fully implemented. They are like interfaces but they have defined bodies to some of the methods BUT usually not all(doesn't matter since you declared it abstract). What it does is allow you to do some of the work and have the rest done by the derived class. This is unlike an interface where the derived class has to implement all of the methods(which can sometimes be tedious).
As far as I know, every method defined in an interface is implicitly abstract
(and public
). But if you declare your implementation class as abstract
, you may skip the implementation of one or more methods defined in the implemented interface.
abstract class Sphere implements Sheet {
public double computeArea() {
// do something
}
// skip computePerimeter():
// public double computerPerimeter();
}
精彩评论