-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh11_54_Interfaces.java
More file actions
72 lines (51 loc) · 2.82 KB
/
Ch11_54_Interfaces.java
File metadata and controls
72 lines (51 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class Ch11_54_Interfaces {
static interface Bike{ // Two methods are being used in this interface that is you need to use each of them
int A = 1308; // Can hold properies too.
void speedUp(int Acc);
void speedDown(int Dec);
}
static interface Lights{
public void brakeLight();
}
// Note - interface's methods must be public whike using in a class to avoid error
static class Honda implements Bike , Lights{
int speed = 60;
Honda(){
System.out.println("Initial speed is: " + this.speed);
}
public void speedUp(int Acc){ //Using 1st method
speed = speed + Acc;
System.out.println("\nSpeed after accelerating is: " + speed);
}
public void speedDown(int Dec){ // Using 2nd method
speed = speed - Dec;
System.out.println("\nSpeed after decelerating is: " + speed);
}
public void brakeLight(){
System.out.println("Break light is on...");
}
}
public static void main(String[] args) {
/* Interface - Point which allows to system to meet and interact with each other
In Java , Interface stands for related methods with empty bodies.
That is , interface in Java contains basic methods without their direct implementation anywhere.
Note: Interface can hold properties too.
**** To be noted if implementing methods of interface in another class they must be set to public
Note: If deriving a class from an Interface you need to use all of it's methods or set it abstract to prevent error. */
Honda bike1 = new Honda();
System.out.println("\nBike registration no. " + bike1.A); // Printing property of an Interface
bike1.speedUp(10);
bike1.speedDown(3);
bike1.brakeLight();
// Note - Properties of a Interfaces are non - manipulative that is cannot be changed after/
/* Abstract class vs Interfaces
* A class can be inherited from an single Abstract class multiple inheritance is not allowed
* Whereas interfaces allow you to create classed from multiple interfaces // Shown in Line 14
* Moreover , a class can both extend and implement. Ex. class A extends B implements C{} */
/* In a nutshell:
1. Interfaces are almost similar to abstract class that is it sets standards that a class need to implement if using it.
2. All methods of an Interface needs to be used to avoid error.
3. Interface's method are public by default and they can hold properties too.
4. After implementing an interface , while using it's method in a class , they must be set public. */
}
}