Click here to watch in Youtube :
https://www.youtube.com/watch?v=x8SqfAWYE9E&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Tutorial : What is an Interface (Switch) |
Java Tutorial : What is an Interface (Switch) |
Java Tutorial : What is an Interface (Switch) |
public class Person { private String name; private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } public void switchOnTheBulb() { Switch bulbswitch = new LEDBulbSwitch(); bulbswitch.switchOn(); } public void switchOffTheBulb() { Switch bulbswitch = new LEDBulbSwitch(); bulbswitch.switchOff(); } }
/** * An interface is a group of related methods with empty bodies. */ public interface Switch { void switchOn(); void switchOff(); }
public class LEDBulbSwitch implements Switch { @Override public void switchOn() { LedBulb ledbulb = new LedBulb("'A' shape","2.3 inch"); ledbulb.switchOnTheLedBulb(); } @Override public void switchOff() { LedBulb ledbulb = new LedBulb("'A' shape","2.3 inch"); ledbulb.switchOffTheLedBulb(); } }
public class LedBulb { private String shape; private String diameter; public LedBulb(String shape, String diameter) { super(); this.shape = shape; this.diameter = diameter; } public String getShape() { return shape; } public void setShape(String shape) { this.shape = shape; } public String getDiameter() { return diameter; } public void setDiameter(String diameter) { this.diameter = diameter; } public void switchOnTheLedBulb() { System.out.println(shape +" Led Bulb is switched on"); } public void switchOffTheLedBulb() { System.out.println(shape +" Led Bulb is switched off"); } }
public class InterfaceDemo { public static void main(String[] args) { Person peter = new Person("Peter",28); peter.switchOnTheBulb(); peter.switchOffTheBulb(); } }
'A' shape Led Bulb is switched on 'A' shape Led Bulb is switched off
https://sites.google.com/site/javaee4321/java/InterfaceDemoSwitchApp.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment