Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class / method

 
janne kallio
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wondering about the following code:
In one file I have got the code:
package food;
public abstract class Fruit {
abstract void setColor();
abstract void setWeigh();
}
and in JC26112003_6.java file I have the code as follows;
import food.Fruit;
class Apple extends Fruit {
void setColor(){}
void setWeigh(){}
}
public class JC26112003_6 {
}
Why am I getting compiler error out of this?
Apple should be declared abstract
Grateful to your explanation!
janne
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by janne kallio:
Wondering about the following code:
In one file I have got the code:
package food;
public abstract class Fruit {
abstract void setColor(); //default modifier
abstract void setWeigh(); //default modifier
}
and in JC26112003_6.java file I have the code as follows;
import food.Fruit;
class Apple extends Fruit {
void setColor(){}
void setWeigh(){}
}
public class JC26112003_6 {
}
Why am I getting compiler error out of this?
Apple should be declared abstract
Grateful to your explanation!
janne

The access modifier of the abstract methods of class Fruit is default. This means that classes and subclasses of Fruit outside package food cant access the methods.
 
janne kallio
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package food;
public abstract class Fruit {
abstract void setColor(); //default modifier
abstract void setWeigh(); //default modifier
}

The access modifier of the abstract methods of class Fruit is default. This means that classes and subclasses of Fruit outside package food cant access the methods.
Cathy, are you saying that though I declared my Fruit class as public, it is considered as default access modifier, not as public modifier.
janne
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cathy, I saw your post at the Certification result forum and would like to congratulate you. Awesome score!
Jane, if i may,...member accessibility is governed separately from class accessibility.
 
janne kallio
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I got it.
Thanks! I haven't yet reached that part in the S&B book. That's why the confusion.
Congratulations, Cathy, for your excellent score!
janne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic