mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 18:11:38 +00:00
Merge pull request #521 from gizmo385/master
Java: Added section on interfaces. Updated array creation information.
This commit is contained in:
commit
3dd569f7cb
@ -101,14 +101,17 @@ public class LearnJava {
|
|||||||
|
|
||||||
// Arrays
|
// Arrays
|
||||||
//The array size must be decided upon instantiation
|
//The array size must be decided upon instantiation
|
||||||
//The format for declaring an array is follows:
|
//The following formats work for declaring an arrow
|
||||||
//<datatype> [] <var name> = new <datatype>[<array size>];
|
//<datatype> [] <var name> = new <datatype>[<array size>];
|
||||||
|
//<datetype> <var name>[] = new <datatype>[<array size>];
|
||||||
int [] intArray = new int[10];
|
int [] intArray = new int[10];
|
||||||
String [] stringArray = new String[1];
|
String [] stringArray = new String[1];
|
||||||
boolean [] booleanArray = new boolean[100];
|
boolean boolArray [] = new boolean[100];
|
||||||
|
|
||||||
// Another way to declare & initialize an array
|
// Another way to declare & initialize an array
|
||||||
int [] y = {9000, 1000, 1337};
|
int [] y = {9000, 1000, 1337};
|
||||||
|
String names [] = {"Bob", "John", "Fred", "Juan Pedro"};
|
||||||
|
boolean bools[] = new boolean[] {true, false, false};
|
||||||
|
|
||||||
// Indexing an array - Accessing an element
|
// Indexing an array - Accessing an element
|
||||||
System.out.println("intArray @ 0: " + intArray[0]);
|
System.out.println("intArray @ 0: " + intArray[0]);
|
||||||
@ -405,6 +408,46 @@ class PennyFarthing extends Bicycle {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Interfaces
|
||||||
|
//Interface declaration syntax
|
||||||
|
//<access-level> interface <interface-name> extends <super-interfaces> {
|
||||||
|
// //Constants
|
||||||
|
// //Method declarations
|
||||||
|
//}
|
||||||
|
|
||||||
|
//Example - Food:
|
||||||
|
public interface Edible {
|
||||||
|
public void eat(); //Any class that implements this interface, must implement this method
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface Digestible {
|
||||||
|
public void digest();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//We can now create a class that implements both of these interfaces
|
||||||
|
public class Fruit implements Edible, Digestible {
|
||||||
|
public void eat() {
|
||||||
|
//...
|
||||||
|
}
|
||||||
|
|
||||||
|
public void digest() {
|
||||||
|
//...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//In java, you can extend only one class, but you can implement many interfaces.
|
||||||
|
//For example:
|
||||||
|
public class ExampleClass extends ExampleClassParent implements InterfaceOne, InterfaceTwo {
|
||||||
|
public void InterfaceOneMethod() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InterfaceTwoMethod() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Further Reading
|
## Further Reading
|
||||||
|
Loading…
Reference in New Issue
Block a user