Java Method Reference in Multithreading

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

//Method reference using static method

interface MyInterface1
{
      public void display();
}
class Test
{
      public static  testShow()
      {
          System.out.println("Hello This is Test Show Method of Test Class");
      }
}


class TestMethodReference
{
      public static void main(String args[])
      {
            MyInterface1 M=Test::testShow;
             M.display();

           
      }
}


// class Test1
// {
//       public static void testShow1(int n)
//       {
//           System.out.println("Hello This is Test Show Method of Test1  Class Bye Bye");
//       }
// }

 //  MyInterface1 M1=Test1::testShow1;
            //  M1.display();

Program 2

//Method reference using instance 
interface MyInterface2 
{
      void display();
}
class Test2 
{
         public void testshow2()
         {
              System.out.println("This is test show 2 method of Test 2 class");
         }
}


class TestMethodReference1
{
      public static void main(String args[])
      {
                Test2 t1=new Test2();
            MyInterface2 M1=t1::testshow2;
            M1.display();
      }
}

Program 3

interface MyButton
{
       void myClick();
}
class MyColor
{
      public static void colorchange()
      {
          System.out.println("Now Color is red .... Thanks");
      }
}
class MyPhoto
{
       public static void photochange()
      {
          System.out.println("Now This is My Photo .... Thanks Bye Bye");
      }
}
class TestButton
{
     public static void main(String args[])
     {
          MyButton B1=MyColor::colorchange; 
                           B1.myClick();
          MyButton B2=MyPhoto::photochange; 
          B2.myClick();
     }
}

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *