• Post Reply Bookmark Topic Watch Topic
  • New Topic

reference of abstract class  RSS feed

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some one please tell me that i passed my abstract class private final reference to another concrete class and i used abstract class reference as parameter to that concrete class constructor and in my main method and null to that parameter then only that program executes correctly...i placing my code below ..if there is any error tell me where is error occcuring then i will check my code...i think my code is right but little bit doubt
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why should you not use a parameter typed to an abstract class. Then runtime type of the argument will be different (either a concrete subtype or an anonymous subtype), so the compiler is programmed to permit abstract types be declared anywhere. Your null could have been any type which is a subtype of your abstract class, so the compiler will permit it.

Don't use parent as an identifier; the word has specific meanings and it does not have to do with subtypes. Always start class names with Capital Letters.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look what Mike Simmons says here about the compiler knowing about runtime types.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In interview test they asked below and i answered like that which i will produce below.test is bug finding and fixing.i will write my explanation also..please tell me is it correct or not..they did n't give feed back to me till now.i want to see my code is it correct or not.They gave 4 classes and first i will submit first class with answer.first programanswer which i submitted is
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bit about final and anId doesn't make sense, I am afraid. Not using spaces after full stops makes it harder to read.
You were right to correct that method, but your explanation is probably mistaken.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i write different possibilities that is in my exam they gavei correct it like
for that correction i wrote comment like that final can't be assigned to id=id .another possibility is if we place same as id=id then answer will produce null which is wrong.your right wrote some mistake in explanation
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any problem to me at inteview point for my exclamation.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I explained in your other thread, there is a flaw in the logic of your setId method.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Joanne implies, this.id = id; is correct. You just haven't explained the error correctly.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:As Joanne implies, this.id = id; is correct. You just haven't explained the error correctly.

Actually I was referring to the implementation of the setId method in this post. It has a logical flaw which I explained in the other thread.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as per my knowledge i corrected it yesterday.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:as per my knowledge i corrected it yesterday.

Try writing some test code.
Create a concrete class that extends the Concept class.
Create two instances of that class.
Call the setId method on one of them passing null as a parameter - put this call in a try/catch block to catch the exception that is thrown.
After the catch block, check if the first instance is equal to the second. Then check if the second instance is equal to the first.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i observed so many things ...if we call any thing on null then NullPointerException will be thrown...here also same thing happening...c,c1 are two instances and if i take c.setId(null) then NullPointerException will be thrown if i call equals method on c like c.equals(c1)..if i call setId() using c1 same thing happening again.i'm placing my code below tell me any thing wrong on that.first i'm placing abstract classconcrete class
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suggest me any website or book to improve my coding skills...where can i find series of tests regarding to bug finding code fixing tests.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote: . . . Actually I was referring to the implementation of the setId . . .
You mean id = id;?
I suspect we are both talking about the same thing, but coing from opposite directions.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:suggest me any website or book to improve my coding skills...where can i find series of tests regarding to bug finding code fixing tests.

Winston already has, but you appeared to have ignored it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:if we call any thing on null then NullPointerException will be thrown...if i call equals method on c like c.equals(c1)

Exactly. But if you fix the logical flaw in your setId method, then the equals method will not throw an exception and you won't need to wrap it in a try/catch
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Joanne Neal wrote: . . . Actually I was referring to the implementation of the setId . . .
You mean id = id;?
I suspect we are both talking about the same thing, but coing from opposite directions.

No
I was referring to this bit of code


A bonus for you sujesh. If Campbell still can't see the logical flaw in this method, I will tell you what it is (which i have already done in the other thread) and tell you how to fix it.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does correcting that error actually make any semantic difference?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Does correcting that error actually make any semantic difference?

No. It will just stop the equals method from ever throwing a NullPointerException
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That probably is a semantic difference, even though the chance of its occurring is small.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. I think we're talking about different things.

If null is passed to this method, I think it is a little illogical to throw an exception that says id must not be null, when on the previous line you've just set id to null.
If you then call the equals method on this instance, you will get a NullPointerException thrown because id is null.

The code should be
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!