




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
it a complete handout for Java Course and will help you in Java
Typology: Lecture notes
1 / 186
This page cannot be seen from the preview
Don't miss anything!





























































































Using the keyword interface, you can fully abstract a class’ interface from its implementation.
That is, using interface, you can specify what a class must do, but not how it does it.
Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body.
To implement an interface, a class must create the complete set of methods defined by the interface.
However, each class is free to determine the details of its own implementation.
You cannot instantiate an interface.
An interface does not contain any constructors.
All of the methods in an interface are abstract.
An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
An interface is not extended by a class; it is implemented by a class.
An interface can extend multiple interfaces.
interface Callback {
void callback(int param); }
Once an interface has been defined, one or more classes can implement that interface.
class classname [extends superclass] [implements interface [,interface...]] {
// class-body}
If a class implements more than one interface, the interfaces are separated with a comma.
The methods that implement an interface must be declared public.
class Client implements Callback { // Implement Callback's interface public void callback(int p) { System.out.println("callback called with " + p); } } Notice that callback( ) is declared using the public access specifier.
One interface may inherit another interface. The inheritance syntax is the same for classes and interfaces. interface MyInterface1 { void myMethod1(…) ; } interface MyInterface2 extends MyInterface1 { void myMethod2(…) ; } When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.
When a class implements an interface that inherits another interface, it must
provide implementations for all inherited methods:
class MyClass implements MyInterface2 {
void myMethod1(…) { … }
void myMethod2(…) { … }
…
}
MyClass must implement all of A and B methods: class MyClass implements B { public void meth1() { System.out.println("Implement meth1()."); } public void meth2() { System.out.println("Implement meth2()."); } public void meth3() { System.out.println("Implement meth3()."); } }
Create a new MyClass object, then invoke all interface methods on it:
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}