-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.java
More file actions
44 lines (34 loc) · 871 Bytes
/
interface.java
File metadata and controls
44 lines (34 loc) · 871 Bytes
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
// Define the interface
interface Animal {
void sound(); // Abstract method
void eat(); // Abstract method
}
// Implement the interface
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
public void eat() {
System.out.println("Dog eats bones");
}
}
// Implement the interface
class Cat implements Animal {
public void sound() {
System.out.println("Cat meows");
}
public void eat() {
System.out.println("Cat eats fish");
}
}
// Main class
class interface{
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound(); // Output: Dog barks
dog.eat(); // Output: Dog eats bones
Animal cat = new Cat();
cat.sound(); // Output: Cat meows
cat.eat(); // Output: Cat eats fish
}
}