This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

Abstract class

 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface Animal {
void travel() {}
void feed() {}
}
class Mammal implements Animal {
void travel() {}
)
Except implementing feed() in Mammal, I'm not able to figure out why should I give 'public' access modifier in class Mammal for both methods. Shouldn't it take default A.M i.e 'friendly' from interface?
Also, accroding to my understanding
An abstract class must contain atleast one abstract method.
If atleast one method in a class is abstract the class should be declared as an abstarct class.
please Correct me if I'm wrong!
any comments on this error?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, all methods defined in an Interface are implicitly public. Therefore, when you define them in a class that implements that interface, they must be public as well.
Secondly, an abstract class DOES NOT need to have an abstract method. Such a case simply defines a class which can not be instantiated. However, if a class has at least 1 abstract method, the class MUST be defined abstract.
I hope that helps,
Corey
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey!
I was under the impression that if you don't have any modifier specified by deafult it is 'friendly'. I guess it true only for classes and not for interface methods!
Thanks
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than "friendly," most people refer to members without an access modifier as having either "default" or "package" access. I don't think I've ever heard anyone refer to it as "friendly" before.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"friend" is a C++ thing. "default", "package", or "package-default" are used in Java.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic