Interface Polymorphism Example

Robot.java

package com.zakilive.ooplearningbyme;

public class Robot implements Walkable {
    public void walk(){
        System.out.println("Robot Walking");
    }
}

 

Human.java

package com.zakilive.ooplearningbyme;

public class Human implements Walkable {
    public void walk(){
        System.out.println("Human walking");
    }
}

 

Walkable.java it is interface

package com.zakilive.ooplearningbyme;

public interface Walkable {

    public void walk();
}

 

Application.java it is main class to print

package com.zakilive.ooplearningbyme;

public class Applicatrion {
    public static void main(String[] args) {
        Human tom=new Human();
        tom.walk();

        Robot wale=new Robot();
        wale.walk();

        walker(new Walkable() {
            @Override
            public void walk() {
                System.out.println("Custom object walking....");
            }
        });

    }

    public static void walker(Walkable walkableEntity){
        walkableEntity.walk();
    }


}

Turning Application main class into lambda:

package com.zakilive.ooplearningbyme;

public class Applicatrion {
    public static void main(String[] args) {
        Human tom = new Human();
        tom.walk();

        Robot wale = new Robot();
        wale.walk();


        //type must be a functional interface and in functional interface there minimum one abdstract method which cannot have a body
       ALambdaInterface aBlockOfCode = () -> {
            System.out.println("Custom object walking....");
            System.out.println("the object tripped....");
        };


//        walker( () -> {
//            System.out.println("Custom object walking....");
//            System.out.println("the object tripped....");
//        });

    }


    public static void walker(Walkable walkableEntity){
        walkableEntity.walk();
    }


}


 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.