-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch_54_interfaces.java
More file actions
76 lines (58 loc) · 1.95 KB
/
Copy pathch_54_interfaces.java
File metadata and controls
76 lines (58 loc) · 1.95 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
73
74
75
76
interface vehicle {
// all methods and data members are public by defaut
int speed = 50;
void brake(int decrement);
void accelerate(int increment);
}
interface showData {
void lowSpeed();
void highSpeed();
}
class cycle implements vehicle, showData {
// int speed=100;
// we can overwrite fields of interface in its derived class
// but not in main function
int dec;
int inc;
public void brake(int decrement) {
dec = decrement;
}
public void accelerate(int increment) {
inc = increment;
}
public void lowSpeed() {
System.out.println("Low Speed : " + (speed - dec));
}
public void highSpeed() {
System.out.println("High Speed : " + (speed + inc));
}
}
public class ch_54_interfaces {
public static void main(String[] args) {
cycle c = new cycle();
c.brake(10);
c.accelerate(15);
c.highSpeed();
c.lowSpeed();
}
}
/*
* Difference B/W abstract class and Interface :
* We can implement a class using Multiple Interface but it cannot be done using
abstract class.
* Interfaces in Java :
* An interface in java is a collection of abstract methods.
* The interface is one more way to achieve abstraction in Java.
* We cannot modify properties of a interface as they are 'final'(constant).
* An interface may also contain constants, default methods, and static methods.
* All the methods inside an interface must have empty bodies except default
methods and static methods.
* We use the interface keyword to declare an interface.
* There is no need to write abstract keyword before declaring methods in an
interface because an interface is implicitly abstract.
* An interface cannot contain a constructor (as it cannot be used to create
objects)
* In order to implement an interface, java requires a class to use the
implement keyword.
* While implementing the interface methods, they need to declared as public.
*/