File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract and interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abstract and interface" Watch "Abstract and interface" New topic

Abstract and interface

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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


Thanks<br />Dinesh
Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
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,

--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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 ]

be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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

Joined: Jan 10, 2007
Posts: 247
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

Joined: Jul 07, 2008
Posts: 76
in Interface all methods are public and abstract
in interface no method can be static, final, native and strictfp
I agree. Here's the link:
subject: Abstract and interface
It's not a secret anymore!