Anonymous Classes in Java

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

Program 1

interface MyButton
{
      void click();
}

class TestAno
{
    public static void main(String args[] )
    {
          MyButton M1=new MyButton()
          {
               public void click()
               {
                     System.out.println("Color is red now");
               }
          };
         MyButton M2=new MyButton(){
               public void click()
               {
                     System.out.println("Hello This is my photo");
               }      
         };
       M1.click();
       M2.click();          
    }
}   

// abstract class MyClass
// {
//      abstract void display();
// }

// class TestAno
// {
//     public static void main(String args[] )
//     {
//             MyClass M1=new MyClass(){
//                   void display()
//                   {
//                       System.out.println("Hello Friends How are you");
//                   }
//             }; 
//             MyClass M2=new MyClass(){
//                   void display()
//                   {
//                       System.out.println("Bye Bye Friends Meet Again........");
//                   }
//             }; 
//             M1.display();
//             M2.display();
//     }
// }

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review 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 *