Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

two public classes in same source file?

 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Today I have given a code


public class A
{
class B b;
}

public class B
{
class A a;
}


and he asked is there any error??

I told him we can't place two public classes in a single source file..He said no it is possible.

then he changes the code as

public class A
{
class B b; // line no1
}

protected class B
{
class A a; // line no2
}

then I said line no1 n line no2 has error.
He was not satisfied, I said this forward declaration is allowed in c++ not in java..

Then at the end of the interview I again said him that its not possible...
he said it is possible either he was joking or I am mad or may be I don't know much....
Please suggest some thing...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They all have errors; you can't just put "class" keywords in random locations.

Other than that--what do you think? What happened when you tried it?

(The idea of "forward declarations" isn't necessary in Java; classes can be defined in any order.)
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:They all have errors; you can't just put "class" keywords in random locations.


Agreed. In order to have a viable debate on a particular of the language, you actually need valid language constructs.

Otherwise, the debate is simple -- Syntax error. Won't compile. Period.

Henry
 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks...
But I don't know what he was trying to do with me...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't either--if that was the exact code you were given, and it's supposed to be Java, it's just plain wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic