Possible Duplicate:
Java: interface / abstract classes / abstract method
I have an abstract class with 4 methods.All these methods are abstract. Is my abstract class equivalent to an interface?
public abstract class ABC{
abstract void f1();
abstract void f2();
abstract void f3();
abstract void f4();
}
Ca开发者_开发百科n anybody explain if it is not?
No. A class can "implement" multiple interface, but can only "extend" one super class, including abstract class.
AFAIK, Java does not support multiple inheritance like C++ does. It however supports multiple implementations (a class can implement multiple interfaces).
Thus, when you inherit from ABC
you won't be able to inherit from any other class. However if you turn it into an interface - you could do that.
Roughly speaking No.
A class can implements
more than an interface at a time but you can extend
just an abstract class.
Abstract classes differ from interfaces in that abstract classes may contain concrete implementation of methods while interfaces may not.
For example, it is legal for abstract classes to have concrete method like this:
public class Abstractclass{
abstract void f1();
/**
* this is a concrete method with implementation
*/
void f2(){
System.out.println("do something");
}
}
But for interface, all methods are implicitly abstract. You cannot have concrete methods:
public interface InterfaceClass{
void f2();
void f3();
}
An interface
is like a contract to the developer with no implementation. It basically specifies the what
. The developer then implements this interface to define the how
.
An abstract
class contains some implementation useful to sub classes.
There a quite a few differences between an abstract class and an interface. I can name a few (I'm not java programmer but C# programmer but reckon these differences still applies).
An interface is not inherited but a abstract class is. If your programming language doesn't support multiple inheritance this will be important.
An interface can not contain code, only signatures but an abstract class can contain code.
Access levels: private, protected, public cant be declared in the interface but in an abstract class.
精彩评论