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
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
--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)
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()!
Joined: Aug 06, 2007
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.