also kindly specify difference b/w them with one brief example .( if you dont mind)
also kindly specify difference b/w them with one brief example .( if you dont mind)
*In interface the methods are only declare not define,but abstract class the methods are define and declare.
*we can not create object for abstract class,but can create object for interface class.
*we should use(define) all the interface class' methods in derived class.if we did not use all methods,the interface class will be abstract class.
the simple and basic differences between abstract and interface are
1) Interface is a pure abstract class where as abstract class not .
2) Abstract class can have one or none abstract methods where as interface have all the methods by default public static absatrct.
3)Interface has attributes by default public static final.
Abstract Class
● An abstract class is a class that contains one or
more abstract methods
● An abstract class cannot instantiated
// You will get a compile error on the following code
MyAbstractClass a1 = new MyAbstractClass();
● Another class (Concrete class) has to provide
implementation of abstract methods
– Concrete class has to implement all abstract methods of
the abstract class in order to be used for instantiation
– Concrete class uses extends keyword
Sample Abstract Class
public abstract class LivingThing {
public void breath(){
System.out.println("Living Thing breathing...");
}
public void eat(){
System.out.println("Living Thing eating...");
}
/**
* Abstract method walk()
* We want this method to be imp
Extending an Abstract Class
● When a concrete class extends the LivingThing
abstract class, it must implement the abstract
method walk(), or else, that subclass will also
become an abstract class, and therefore cannot be
instantiated.
● For example,
public class Human extends LivingThing {
public void walk(){
System.out.println("Human walks...");
}
}
What is an Interface?
● It defines a standard and public way of specifying
the behavior of classes
– Defines a contract
● All methods of an interface are abstract methods
– Defines the signatures of a set of methods, without the
body (implementation of the methods)
● A concrete class must implement the interface (all
the abstract methods of the Interface)
● It allows classes, regardless of their locations in the
class hierarchy, to implement common behaviors
11
Example: Interface
// Note that Interface contains just set of method
// signatures without any implementations.
// No need to say abstract modifier for each method
// since it assumed.
public interface Relation {
public boolean isGreater( Object a, Object b);
public boolean isLess( Object a, Object b);
public boolean isEqual( Object a, Object b);
}
what is recovery testing plz reply me?
[QUOTE=ananth3335;32965]*In interface the methods are only declare not define,but abstract class the methods are define and declare.
*we can not create object for abstract class,but can create object for interface class.
*we should use(define) all the interface class' methods in derived class.if we did not use all methods,the interface class will be abstract class.
/QUOTE]
I thank you sincerely for giving a quick response for my doubts and for clarifying them with such a good example. thanyou once again.
An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.
Hi Friend,
Go through this link its really useful
interface vs abstract class : Java Glossary
Cheers,
Sridevichitthu
In abstract class, the method may be declared with body / without method body. But in Interface the method body cannot be defined.
Abstract class has abstract methods as well as non abstract methods where as in interface has only abstract methods but not marked with abstract. In abstract class there is different flavor of method like public, private etc... But in interface only public methods and public final instance variable with only definition. Where to use abstract class and where interface ============================================ if sub class required extends another class then use interface because java dose not support multiple inheritance.