Plan your text ad campaign.
World class Hard Drive Recovery and renowned raid recovery services
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs and more.
public access modifier for interface methods?
A: It is advisable to design relatively large applications using interfaces because it makes the whole system easier to modify, extend and integrate new features. To start with, you may only have one implementation of a given interface, but if you find you need slightly different behaviour in special circumstances, you only need write a class that conforms to one of the existing interfaces and it will drop in place without major modifications.
Interfaces also allow you to adapt a class from a different hierarchy to work in an existing application. The class only needs to declare it implements the interface, provide the necessary methods and it can be integrated directly as if it were created for the job.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: A Java interface is a definition of a class type without any concrete implementation. Typically, an interface consists of one or more method signatures that a subclass must fulfil to conform to the type. In effect, all their methods are abstract, and interfaces cannot be instantiated.
More details available to premium content service subscribers:
What is the difference between abstract classes and interfaces?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Yes, it is always necessary to create an object implementation for an interface. Interfaces cannot be instantiated in their own right, so you must write a class that implements the interface and fulfil all the methods defined in it.
public class Concrete implements ExampleInterface {
...
}
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: In Java an interface cannot extend an abstract class. An interface may only extend a super-interface. And an abstract class may implement an interface. It may help to think of interfaces and classes as separate lines of inheritance that only come together when a class implements an interface, the relationship cannot be reversed.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
public access modifier for interface methods?
A: Java interfaces are used to define a public Application Programming Interface (API) for classes to implement, so a public modifier is redundant in this context. Non-public modifiers are not valid for interfaces, so the compiler should fail and warn you in this case.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: An application programming interface (API) is the collection of all the public methods and fields that belong to a set of classes, including its interface types. The API defines the way that developers can use the classes in their own Java program, just by importing the relevant classes and writing statements that instantiate the classes and call their methods public fields.
A Java interface declares a type of Java object without a concrete implementation. Interfaces are defined in a compilation unit like a standard Java class, and the methods they declare are implicitly part of an application programming interface. Interfaces are used to help define generic structural elements in an API, a reference type for concrete implementations, and an extension mechanism for programmers who use the API.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Marker interfaces are those which do not declare any required methods, but signify their compatibility with certain operations. The java.io.Serializable interface is a typical marker interface. It does not contain any methods, but classes must implement this interface in order to be serialized and de-serialized.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Interfaces may have member variables, but these are implicitly final and static because they are not inherited by extension. In effect interface variables are constants that are available to all implementations and may be used as key references for method arguments for example.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The way that interface constants are declared is arbitrary, whether you use the full public static final modifiers or none at all the compiled code will be the same. Strictly the modifiers are redundant.
Java code style is intended to help the reader without stopping to think too hard. The convention of setting constant variable names in upper case letters with underscore separators, CONSTANT_NAME, is recommended and likely to be a stronger guide overall. Using the short form in interfaces should be effective, but if some of your readers are novice and likely to miss the point, it may help to spell out the modifiers.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: It may help to discuss an example using birds: an interface called Avian, a superclass FlyingBird that implements Avian and two concrete classes: Parrot and Penguin. Parrot extends FlyingBird, so is implicitly an Avian type, but Penguin does not, it only implements Avian. The examples below work through all possibilities, passing references to methods soundBirdCall(FlyingBird) and displayPlumage(Avian).
More details available to premium content service subscribers:
What are the rules for passing subclasses for method arguments?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Abstract classes are often used to provide methods that will be common to a range of similar subclasses, to avoid duplicating the same code in each case. Each subclass adds its own features on top of the common abstract methods.
More details available to premium content service subscribers:
When should I use abstract classes rather than interfaces?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
final?
A: It is not permitted to declare an interface as final, it will cause a compilation error. It does not make sense to declare an interface final because it contains no implementation code and cannot instantiated in its own right. The final modifier is used to prevent the extension of concrete classes.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The code you are looking at declares an inline anonymous class that implements an interface, which has a similar effect. This declaration does not instantiate the interface, but defines the type of the anonymous class, which has no name of its own. This approach is often used in AWT or Swing applications where a class is required to fulfil a minimal interface and it is not necessary to retain a reference to it by assignment.
More details available to premium content service subscribers:
This code seems to be instantiating an interface!
Actions: Follow-up, clarify or correct this answer. Submit a new question.
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
About us: site help, text ads, sponsored links and premium content.