• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract and interface

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abstract class can have instance methods that implement a default behavior
An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract

Can anyone expalin me the meaning of default behavior and difference between two with example

please
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Dinesh,

First think abstract classes are classes that can have well-defined
methods and some non-implemented methods. Default behavior in my
opinion means well-defined(concrete method implementations). Additionally
abstract class is extended.

If you talk about interfaces, you can't give any method implementations
and interfaces are implemented. For a given class, you can implement
more then one interface.

In case of abstract classes, a class can only extend one class not
multiple classes.

Hope this helps,
Ben
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Abstract class, is essentially a class which can contain abstract methods, and concrete methods. Any class exending an abstract class MUST implement those abstract methods, unless the extending class is also abstract, in which case it does not have to implement the methods.

The rule of thumb in the inheritance chain the first concrete (non abstract class) to extend an abstract class MUST implement those abstract methods.

Abstract classes cannot be instantiated with the new keyword, they must be extended (inherited)

E.g (probably plagarism, apologies!)




In an interfacte, you are essentially creating a "contract" in your application, specifying that any class unless abstract must implement the methods decalred (but not implemebted) in the interface.

Interfaces cannot have non abstract methods, and all variables are static and must be initialized in the interface

So from the example before we can change to an interface:



Interfaces overcome the java restriction of single parent classes, as a class may implement any number of interaces, and interfaces may EXTEND any number of other interfaces.

Hope this helps!
[ September 17, 2008: Message edited by: Stephen Davies ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean to say that we well defined the methods behavior i.e what method will do in case of abstract.
But in interface also we defien the method as what will it do.
One more doubt, all variables in interface are static and final by default and methods are public and abstract by default. Correct me if iam wrong.
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an interface (1) all variables are public, static and final,
(2) all methods are public
(3) no static methods are allowed in an interface.
 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in Interface all methods are public and abstract
in interface no method can be static, final, native and strictfp
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic