-3

Executing the Test class gives a StackOverFlow error :(

Parent class :

package Pack;

public class Alpha {
  protected void sayHi() {
    System.out.println("Hi...from Alpha");
  }
}

Child class :

import Pack.Alpha;

public class AlphaSub extends Alpha {
  AlphaSub sb=new AlphaSub() ;

  void hi() {
    sb.sayHi() ;

  }
}

Test class :

public class test {
public static void main(String[] args) {

    AlphaSub ob=new AlphaSub() ;
    ob.hi() ;
}

}

1 Answers1

2

Your stackoverflow has nothing to do with the inheritance, but with this line (inside AlphaSub):

AlphaSub sb=new AlphaSub() ;

You are creating a new AlphaSub instance inside your AlphaSub, which causes an endleess loop. I assume you don't even make it to the ob.hi() line in your test

Nir Levy
  • 12,750
  • 3
  • 21
  • 38