Documente Academic
Documente Profesional
Documente Cultură
1. The Printable interface has a method, 5. 29. 4. A Partial Interface interface ThatInterface { class Info {
print. Implementation public void thatMethod(); // Definition of the class...
2. print is public even though there is no interface Conversions { } }
public keyword. double INCH_TO_MM = 25.4; }
double OUNCE_TO_GRAM = 28.349523125 class MyClass implements ThisInterface, Tha
; tInterface { public class MainClass {
5. 29. 2. Fields and Methods in an double POUND_TO_GRAM = 453.5924; // Class definition including methods from public static void main(String[] a) {
Interface double HP_TO_WATT = 745.7; both interfaces... Port.Info info = new Port.Info();
1. Fields in an interface must be initialized double WATT_TO_HP= 1.0 / HP_TO_WATT; public void thisMethod() { }
and are implicitly public, static, and public double inchesToMillimeters(double System.out.println("this"); }
final. inches); }
2. You declare methods in an interface public double ouncesToGrams(double oun
just as you would in a class. ces); public void thatMethod() { 5. 29. 9. Encapsulating Constants in a
3. Methods in an interface do not have a } System.out.println("that"); Program
body. } interface ConversionFactors {
4. All methods are implicitly public and } double INCH_TO_MM = 25.4;
abstract 5. 29. 5. Extending Interfaces
interface ConversionFactors { public class MainClass { double OUNCE_TO_GRAM = 28.349523125
interface Conversions{ double INCH_TO_MM = 25.4; public static void main(String[] a) { ;
double inchesToMillimeters(double inches) } MyClass cls = new MyClass();
; interface Conversions extends ConversionFa cls.thisMethod(); double POUND_TO_GRAM = 453.5924;
} ctors { cls.thatMethod();
double inchesToMillimeters(double inches); } double HP_TO_WATT = 745.7;
} }
5. 29. 3. To implement an interface: use double WATT_TO_HP = 1.0 / HP_TO_WATT
the implements keyword after the class OUTPUT: ;
declaration 5. 29. 6. Interfaces and Multiple this }
public class CanonDriver implements Printa Inheritance that ---
ble { interface HisInterface {} public class MainClass {
public void print (Object obj) { interface HerInterface {} public static void main(String[] a) {
// code that does the printing public interface MyInterface extends HisInt 5. 29. 8. Nesting Classes in an Interface System.out.println(ConversionFactors.INC
} erface, HerInterface {} Definition H_TO_MM);
}
} } long randomLong = rand.nextLong() * 10; erface);
} float randomFloat = rand.nextLong() * 10; }
public static void v(C x) { double randomDouble = rand.nextDouble() }
OUTPUT: x.cMethod(); * 10;
25.4 } }
7. 2. 4. If a class object is an interface or
public static void w(ClassA x) { a class
5. 29. 10. Multiple interfaces x.aMethod(); 7. 2. 1. The superclass of interfaces is public class Main {
interface A { } always null public static void main(String[] args) {
void aMethod(); public class Main { // Checking whether Cloneable is an interf
} public static void main(String[] args) { public static void main(String[] argv) throw ace or class
D h = new D(); s Exception { Class clazz = Cloneable.class;
interface B { t(h); boolean isInterface = clazz.isInterface();
void bMethod(); u(h); Class cls = java.lang.Cloneable.class; System.out.println("Is Interface = " + isInt
} v(h); Class sup = cls.getSuperclass(); // null erface);
w(h); } }
interface C { } }
void cMethod(); }
} 7. 2. 5. Listing the Interfaces That a
7. 2. 2. Listing the Interfaces That an Class Implements
class ClassA { 5. 29. 11. Initializing interface fields Interface Extends public class Main {
public void aMethod() { with non-constant initializers public class Main { public static void main(String[] argv) throw
} import java.util.Random; public static void main(String[] argv) throw s Exception {
} s Exception { Class cls = java.lang.String.class;
public class MainClass { Class cls = java.util.List.class; Class[] intfs = cls.getInterfaces();
class D extends ClassA implements A, B, C { public static void main(String[] args) { Class[] intfs = cls.getInterfaces(); // java.u
public void bMethod() { System.out.println(RandVals.randomInt); til.Collection }
} System.out.println(RandVals.randomLong }
); }
public void cMethod() { System.out.println(RandVals.randomFloat
} ); 7. 2. 6. Although the type of o2 is an
} System.out.println(RandVals.randomDoub 7. 2. 3. Checking whether String is an interface, getSuperclass() returns the
le);
interface or class object's superclass
public class MainClass { }
public class Main { public class Main {
public static void t(A x) { }
public static void main(String[] argv) throw public static void main(String[] argv) throw
x.aMethod();
s Exception { s Exception {
}
interface RandVals {
Class clazz = String.class; Runnable o2 = new Runnable() {
public static void u(B x) { Random rand = new Random();
boolean isInterface = clazz.isInterface(); public void run() {
x.bMethod(); int randomInt = rand.nextInt(10);
System.out.println("Is Interface = " + isInt }
};
Class sup = o2.getClass().getSuperclass(); /
/ java.lang.Object
}
}
Class cls = int.class;
Class[] intfs = cls.getInterfaces(); // []
}
}