• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface implemenation class

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source code
==========
interface Inter1{
void im1();
}
class InterClass implements Inter1{
Inter1 ob_ref=new InterClass();//line #1
InterClass(){ im1(); }
public void im1(){ System.out.println("interface method"); }
public void m1(){ System.out.println("Non interface method"); }
}

class InterAnon1{
public static void main(String[] args){
InterClass ob=new InterClass();
ob.im1();
ob.m1();
Inter1 ob1=new InterClass();
ob1.im1();
ob1.m1();
}//main
}//class

if i use statement at line #l it's getting successful compilation but when
i try to run it's printing some thing like
at InterClass.<init>(InterAnon1.java:6)
at InterClass.<init>(InterAnon1.java:6)
at InterClass.<init>(InterAnon1.java:6)
...
what does this mean? Does this means StackOverflow,if so can't we get StackOverflowException?

Thanks .
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you got it right. you should be using just a reference rather than an object.... use just this " Inter1 ob_ref " & declare a method to to initialise the reference (object being passed by say main method)
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I get after launching similar code:

Exception in thread "main" java.lang.StackOverflowError
at edu.scjp.test2.stackoverflow.StackOverflowCheck.<init>(StackOverflowCheck.java:10)
at edu.scjp.test2.stackoverflow.StackOverflowCheck.<init>(StackOverflowCheck.java:10)
at edu.scjp.test2.stackoverflow.StackOverflowCheck.<init>(StackOverflowCheck.java:10)
at edu.scjp.test2.stackoverflow.StackOverflowCheck.<init>(StackOverflowCheck.java:10)
at edu.scjp.test2.stackoverflow.StackOverflowCheck.<init>(StackOverflowCheck.java:10)
at edu.scjp.test2.stackoverflow.StackOverflowCheck.<init>(StackOverflowCheck.java:10)
at edu.scjp.test2.stackoverflow.StackOverflowCheck.<init>(StackOverflowCheck.java:10)
... (and so on)

So StackOverflowError (not Exception, usually JVM crashes result in throwing subclasses of Error rather than Exception) is shows up as it should... Are you using Sun's JRE?

To further explain: the statement 'Inter1 ob_ref=new InterClass();' is a kind of one-line initialization block. It will be executed each time a new instance of your InterClass is created, right before the constructor runs. It is very much like instantiating a new instance of the class in the constructor itself, which obviously leads to stack overflow.
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one explain why we are getting a StackOverflowError...

I still didn't get it.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can't do "ob1.m1();", because m1() method is not part of the interface..
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are getting StackOverflowError because of a recursive instatiation of an InterClass object where it has #line 1.

When you instantiate a InterClass, it will instantiate a instance variable also of InterClasss, and this instance variable will also have a instance variable os InterClass being instantiated.. and it will keep going, until you get StackOverflowError.

I hope it helps, if not, say and I'll try to make the point more clear.

By the way, try to use the code like this next time:


Kind Regards,
Raphael Rabadan
[ July 29, 2008: Message edited by: Raphael Rabadan ]
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could not follow it still. Kindly illustrate it a bit graphically.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raphael Rabadan:
You are getting StackOverflowError because of a recursive instatiation of an InterClass object where it has #line 1.

When you instantiate a InterClass, it will instantiate a instance variable also of InterClasss, and this instance variable will also have a instance variable os InterClass being instantiated.. and it will keep going, until you get StackOverflowError.

I hope it helps, if not, say and I'll try to make the point more clear.

By the way, try to use the code like this next time:




Let me see if I got that correct.
The construtor runs with the first statment InterClass ob = new InterClass();
Before the body gets executed it is supposd to instantiate the reference variable.
here the reference varaible is Inter1 ob_ref = new InterClass();// line #1
Here,to instantiate the variable it calls the constructor again which inturns tries to instantiate the reference variable and this process continues resluting in Stack Over flow Error.

Anand - This should make it a litttle clear.

Thanks , Raphael
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, thanks.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anand Shrivastava:
I could not follow it still. Kindly illustrate it a bit graphically.



Look at this
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ankit. I followed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic