Tuesday, 1 December 2015

Java Tutorial : Java method returning Interface


Click here to watch in Youtube :
https://www.youtube.com/watch?v=IfI7n6pV_4A&list=UUhwKlOVR041tngjerWxVccw

Person.java
public interface Person
{

    public void walk();
}
Student.java
public class Student implements Person
{

    private String name;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @Override
    public void walk()
    {
        System.out.println(name + " is Walking...");

    }

}
StudentTest.java
public class StudentTest
{

    public static void main(String[] args) throws InterruptedException
    {

        Person johnReferenceVariable = getPerson("John");

        johnReferenceVariable.walk();

        System.out.println("--------------------------------");

        Person peterReferenceVariable = getPerson("Peter");

        peterReferenceVariable.walk();

    }

    /*
     * You also can use interface names as return types. In this case, the
     * object returned must implement the specified interface.
     */
    public static Person getPerson(String name)
    {
        Student student = new Student();
        student.setName(name);
        return student;
    }

}
Output

John is Walking...
--------------------------------
Peter is Walking...

Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/MethodDemoReturnInterfaceApp.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/MethodDemoReturnInterfaceApp

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/68054fa19b88e552162495a83ba964283753af15/BasicJava/MethodDemoReturnInterfaceApp/?at=master

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