abstract class or interface
public class C {
public abstract Boolean methodC();
}
Cla开发者_StackOverflow社区ss A implements C {
// all non-abstract methods
}
Class B extends A {
// all non-abstract methods
}
In Java, can
C
be an abstract class or does it have to be an interface? [NOTE: It says - 'A IMPLEMENTS C'. Can you IMPLEMENT an abstract class in Java?]
Here are some descriptions in lay terminology to help:
An Interface: This is a collection of methods. They have no definition whatsoever and their function is determined by the class that implements them. An example of an interface is a List. All lists (ArrayList, LinkedList) have add() and remove() methods because they implement the List interface, which demands them.
public interface List {
public void add (Object o);
public void remove (Object o);
}
public class MyList implements List {
public void add (Object o) {
// I must implement this method because of the interface List
}
public void remove (Object o) {
// I must implement this method because of the interface List
}
}
An Abstract Class: This is a partially completed class. It is different from an Interface in that it often contains some functionality by itself. However, it (generally) is missing some methods that need to be defined by an *extend*ing subclass. These methods are defined as abstract methods in the abstract super class.
public abstract class AbstractThing {
public void method1 (Object o) {
// This is a real method that does things
}
// Anyone who extends me must implement this
public abstract void method2 (Object o1, Object o2);
}
public class ActualThing extends AbstractThing implements List {
public void add (Object o) {
// I must implement this method because of the interface List
}
public void remove (Object o) {
// I must implement this method because of the interface List
}
public void method2 (Object o1, Object o2) {
// I must implement this method because of abstract method in
// the super class
}
The implements keyword is used by a class to indicate that it is going to implement the methods demanded by an interface. A class can implement as many interfaces as it likes; the only requirement is that it provides a definition for each of those methods. It can also be defined as abstract and rely upon its subclasses to define some. Then, those methods are just like the abstract methods of any other abstract class.
The extends keyword is used by a class to indicate that it is going to add functionality to some existing class. If the parent class is abstract, the extending class must implement any of those abstract methods in the parent. A class can only extend one parent class.
Why all this complexity? Interfaces are nice because you can assume certain functionality of implementing classes. Specific case is the List interface. Most methods don't care what kind of list they get; they just want to know that the object supports the common list methods. In the statement:
List<String> myList = new ArrayList();
You are creating an actual ArrayList object, but hiding it's implementation under the List interface. Then, later, if you decide to use a LinkedList instead, you don't have to change all of your code since it is also implementing the List interface.
There are also several 'interface sets' that Java defines (e.g. persistence) but does not implement. This allows 3rd parties to develop their own implementations (actual classes) but since they all implement the same common interface, developers can swap implementations in and out without changing the code.
class A cannot implement C, because C is not an interface.
Let's understand it, An interface in java is a blueprint of a class. It has static constants and abstract methods only.
The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java.
Java Interface also represents IS-A relationship.
It cannot be instantiated just like abstract class.
As shown in the figure given below, a class extends another class, an interface extends another interface but a class implements an interface.
class A cannot implement C, because C is not an interface. You extend classes; you implement interfaces.
C looks like it can be either an abstract class or an interface, because you don't have a default behavior for that method.
In Java, each class can only extend 1 other class, but it can implement as many interfaces as it want.
C can be either an interface or an abstract class.
C could be either; if it were a class it'd have to be declared as a "public abstract class."
If C is an abstract class, then A should extend C. Also, you need to use the word abstract before the word class while defining C.
If C is an interface, then remove the word "abstract" before methodC.
Any type with all abstract methods is normally an interface but there is nothing stopping it from being an abstract class. So, C in your example can be a class, as you have it. More commonly it would be an interface.
Your A class states it implements C. Classes can only implement interfaces. So, if you want to keep C a class, then A will need to extend C. If you make C an interface, then the use of implements would be correct.
Here's a summary of the rules:
- An interface can
extend
any number of interfaces. - A class can optionally
extend
exactly one class; it extendsObject
if you don't specify anything. - A class can
implement
any number of interfaces. - Any class can be declared
abstract
. - Any class that has abstract methods must be declared
abstract
. - Any class that extends an abstract class or implements an interface must implement all abstract and interface methods or else must be declared
abstract
.
精彩评论