Polymorphism in Interfaces In Java

 package com.company;


interface Camera{
void takeSnap();
void recordVideo();
default void record4KVideo(){
// We can override this default method in the class but then this method will not work
// instead the method in class will work.
System.out.println("Recording In 4k ...");
}
}

interface Wifi{
String [] getNetwork();
void connectToNetwork(String network);
}


class CellPhone{
void callNumber(int phoneNumber){
System.out.println("Calling"+ phoneNumber);
}
void pickCall(){
System.out.println("Connecting ...");
}


}

class SmartPhone extends CellPhone implements Wifi, Camera{
public void takeSnap(){
System.out.println("Taking Snap");
}
public void recordVideo(){
System.out.println("Taking Videp");
}
public String[] getNetwork(){
System.out.println("Getting the list of networks ...");
String[] networkList = {"Digisol", "UCN_Dr_Sonone"};
return networkList;
}
public void connectToNetwork(String network){
System.out.println("Connecting to "+ network + " ...");
System.out.println("Connected");
}

}

public class tuts {
public static void main(String[] args) {
Camera cam = new SmartPhone();
// cam.getNetwork(); --> not allowed
cam.record4KVideo(); // This is a smartphone but use it as a camera.
}
}

Comments

Popular posts from this blog

Creating a Thread by Extending Thread class

Constructors from Thread class in Java