-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP_interface.java
More file actions
38 lines (31 loc) · 1.4 KB
/
Copy pathOOP_interface.java
File metadata and controls
38 lines (31 loc) · 1.4 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
/* Interface: Interface is a collection of abstract methods. We can't intantiate an interface. Interface contains only
final fields and abstract methods, but not regular methods. Interface also doesn't contain any constructor.
We use the 'implements keyword in interface code, not the 'extends'
*/
interface Animal // Interface
{
public abstract void catSound(); // abstract method;
} // Regular methods cannot exist in interfaces.
class Cat implements Animal // Use the 'implements' keyword for interface
{
public void catSound() // Body of catSound() is provided here
{
System.out.println("Meow");
}
}
public class OOP_interface
{
public static void main(String[] args)
{
Cat obj = new Cat();
obj.catSound();
// Animal obj1 = new Animal(); Will give error: Cannot instantise Animal
}
}
/*
Why And When To Use Interfaces?
1) To achieve security - hide certain details and only show the important details of an object (interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one superclass).
However, it can be achieved with interfaces, because the class can implement multiple interfaces.
Note: To implement multiple interfaces, separate them with a comma.
*/