-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy path31_InterfaceExample.java
More file actions
56 lines (42 loc) · 1.05 KB
/
31_InterfaceExample.java
File metadata and controls
56 lines (42 loc) · 1.05 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
/**
* Interface Example One
* */
public class Main {
public static void main(String[] args) {
// new Animal(); // Not Allowed.
Animal animal = new Dog();
animal.eat();
animal.run();
}
}
interface Animal {
public static final float pi = 3.14f;
abstract public void run();
abstract public void eat();
}
class Dog implements Animal {
@Override
public void run() {
System.out.println("Dog is running");
}
@Override
public void eat() {
System.out.println("Dog is eating");
}
}
/*
* Rules of Interface
* * You cannot create object of interface. They are just a blueprint of a class.
* * Use implements keyword
* * They can only contain abstract methods.
*
* 1. For Methods
* * They are public and abstract
* * No method body. You cannot write code within your method.
*
* 2. For Variables
* * Avoid using field variables
* * But when used they are constants. Their values cannot be changed once defined.
* * They are public, static and final
*
* */