Java Program causes infinite loop

java class interface

43 просмотра

1 ответ

This is my Java code. I have created one interface abc with two classes. class a1 implements interface abc. Class b1 uses the interface function display to show some data.

class a1 runs in an infinite loop.

interface abc
{
  display(String s);
}

class a1 implments abc
{
   a1(b1 obj)
  {
  }
    public void display(String s)
    {
        System.out.println(s);
    }
}

class b1
{
    abc abc1;
    private xyz x;
    b1(xyz xyz1)    //xyz is interface
    {
           this.x = xyz1; 
    }
    public void show()
    {
          abc1 = new a1(new b1(this.x));   //  here is problm.. this statement cause infinite loop.
          String str = "Hello"; 
          abc1.display(str);
    }
}

This program causes infinite loop of class a1. Please find the problem and help me solve.

Автор: Mohit Singla Источник Размещён: 08.11.2019 11:00

Ответы (1)


0 плюса

It has no infinite loop. I have created a main and called b1.show and it is running successfully.

interface abc
{
   void display(String s);
}

class a1 implements abc
{  
   a1(b1 obj)
  {
}
  public void display(String s)
  {
     System.out.println(s);
  }
}

class b1
{
   abc abc1;
   private xyz x;
   b1(xyz xyz1)    //xyz is interface
   {
       this.x = xyz1; 
}
    public void show()
    {
        abc1 = new a1(new b1(this.x));   //  here is problm.. this statement cause infinite loop.
        String str = "Hello"; 
        abc1.display(str);
   }
}
public class Main{
    public static void main(String args[]){
      xyz xyz1 = null;
        b1 objb1=new b1(xyz1); 
      objb1.show();
      System.out.println("out of all classes..");
  }

}

interface xyz{

}
Автор: Rahul Agrawal Размещён: 20.08.2016 07:45
Вопросы из категории :
32x32