Thursday, 27 August 2015

Java Tutorial : What is an Interface (Switch)


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)
Person.java
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();
    }
    
}
Switch.java
/**
 * An interface is a group of related methods with empty bodies.
 */
public interface Switch
{
    void switchOn();

    void switchOff();
}
LEDBulbSwitch.java
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();
    }

}
LedBulb.java
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");
    }

}
InterfaceDemo.java
public class InterfaceDemo
{

    public static void main(String[] args)
    {
        Person peter = new Person("Peter",28);
        peter.switchOnTheBulb();
        peter.switchOffTheBulb();
    }
}
Output
'A' shape Led Bulb is switched on
'A' shape Led Bulb is switched off
To Download InterfaceDemoSwitchApp Project Click the below link
https://sites.google.com/site/javaee4321/java/InterfaceDemoSwitchApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge