This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Abstract and interface

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in Interface all methods are public and abstract
in interface no method can be static, final, native and strictfp
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic