• Post Reply Bookmark Topic Watch Topic
  • New Topic

constructor  RSS feed

 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

some one please tell me that in abstract class constructors are not recommended since we don't call it directly ...my doubt is below code is right or wrong...
3 public abstract class Concept
4 {
5 private String id;
6
7 protected Concept( String anId )
8 {
9 if ( anId == null )
10 {
11 throw new NullPointerException( "id must not be null" );
12 }
13
14 id = anId;
15 }
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always put your code between the specified code tags

It's at the top when you go to post a new topic

It will just make your code easier to read
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even though i used code tab that displays same....may i have to remove space between lines...please sorry next time i won't repeat that.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code tags do not work as expected because you typed:

[code=java][/code]
3 public abstract class Concept
4 {
5   private String id;
6
7   protected Concept( String anId )
8   {
9     if ( anId == null ) 
10     {
11       throw new NullPointerException( "id must not be null" );
12     }
13
14     id = anId;
15   }


And you should have typed

[code=java]
3 public abstract class Concept
4 {
5   private String id;
6
7   protected Concept( String anId )
8   {
9     if ( anId == null ) 
10     {
11       throw new NullPointerException( "id must not be null" );
12     }
13
14     id = anId;
15   }
[/code]


Also, plese do not post line numbers with your code. They will be added automatically.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On your original question: there's nothing wrong with having a constructor in an abstract class. If the class has state that ought to be initialised then a constructor may well be the right place to do that.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote: . . .
. . .
Or you can write
Objects.requireNotNull(anId, "anId must not be null" +
        " in setId method");
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!