• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract class  RSS feed

 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some one please tell me which mistake i done in my below code

3 public abstract class Feature
4 {
5 private String age;
6
7 protected Feature( String page )
8 {
9 if ( page == null )
10 {
11 throw new NullPointerException( "age must not be null" );
12 }
13
14 age= page;
15 }
16
17 public String getAge()
18 {
19 return age;
20 }
21
22 public void setAge( final String age )
23 {
24 age = age;
25 }
26
27 public boolean equals( Object o )
28 {
29 return o != null && o.getClass().equals( getClass() ) && age.equals( ( (Feature) o ).age);
30 }
31
32 public String toString()
33 {
34 return "Feature(" + age + ")";
35 }
36 }
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you be more specific about what the problem is? A compiler error? Then post the error.

And please use the code tags for posting code.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: . . .
And please use the code tags for posting code.
Without line numbers, please. The code tags add the line numbers automatically.
 
Ravi Shankar Jha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
every thing seems right.. but except for a small thing at line 24. you should use this.age = age.

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Ravi Shankar Jha
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i corrected it later...why final variables can't assigned like age=age;
 
Ravi Shankar Jha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
once the age is declared final you cannot reassigned it.
compiler views age = age, as you are reassigning the value of age to itself, so you will get compile time error.
To overcome this scenario you need to specify for which age you are setting the value.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for final variables is first time assignment possible or not..my lecturer said first time assignment possible for final variables,so i assigned like that.
 
Ravi Shankar Jha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we will call setAge method passing a String parameter at that time first assignment will happen.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for first time assignmet which value it takes...is it null value.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:for final variables is first time assignment possible or not..my lecturer said first time assignment possible for final variables,so i assigned like that.
Have you tried it? Which variable is final?
What do you mean by first time assignment? You should know whether local variables, fields or parameters have default values, and what they are. If you have forgotten, please go to the Java Tutorials or Java Language specification and refresh your memory. I could not find the right sections, but try looking for the sections about definite assignment.

You have been told several times about age = age; Have you really not found out what the error in that method is?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!