-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphimsInInterface.java
More file actions
53 lines (49 loc) · 1.61 KB
/
PolymorphimsInInterface.java
File metadata and controls
53 lines (49 loc) · 1.61 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
/*
to restrict the usage of certain methods in class
we create the refernce of the interface those methods we want to use and our class have implemented it
means the reference nwo only call to the methods which are avaialbe in interface.
*/
import java.util.Scanner;
interface Bluetooth{
void turnOn();
void getNetworks();
}
interface Camera{
void takePicture();
void captureVideo();
default void addTimeStamp(){
System.out.println("Added timestamp");
}
}
interface GPS{
String[] getNetworks();
void setNetworks(String networks[]);
}
class SmartPhone implements Camera,GPS{
private String[] networks;
public void takePicture(){
System.out.println("Captured picture");
}
public void captureVideo(){
System.out.println("Started recording....");
}
public String[] getNetworks(){
return networks;
}
public void setNetworks(String networks[]){
this.networks=networks;
}
public void greet(){
System.out.println("good morning");
}
}
public class PolymorphimsInInterface {
public static void main(String[] args) {
Camera camera; // here we create the reference of interface
camera=new SmartPhone(); //created object of SmartPhone class and stored the addres of it in the refernece of Camera interface.
camera.takePicture(); // this is allowed
// camera.getNetworks(); // this is not allowed
camera.addTimeStamp();// thsi is alsow allowed
// Bluetooth bluetooth=new SmartPhone(); this will not work bcoz smartphone never implemented Bluetooth interface
}
}