Abstract keyword is used to define a method as abstract or any class as abstract. A class is defined as abstract when it contains at least one method as abstract. It can contain the other methods that are not abstract. Well interfaces are classes which have all the methods that are not implemented. These came to arise because of the indirect implementation of Multiple Inheritance. The interfaces are designed to implement multiple inheritance. And the abstract classes can be extended by a wide variety of classes that implement that abstract methods. Hope i explained the query correctly.. Bye Venu
Originally posted by Anuja: What is Abstract keyword and Interfaces? Would you please explained by an example? Thanks a lot! Regards, Anuja
Hi Anuja, I suppose the difference in the definition is due to the constraint in java of multiple inheritance. The multiple inheritance is done or rather achieved in java by the use of interfaces. In this way you can do multiple inheritance. Bye Venu
As per my understanding 1. Both interfaces & abstract classes cannot be explicitly instantiated. 2. Even a single method being defined abstract makes the class abstract. 3. All the methods of an interface HAVE to implemented by the implementor. 4. Unlike a interface, a method of an abstract class can have default implementation. 5. A class can extend a single parent but can implement multiple interfaces. 6. The only thing that a class can inherit from an interface is the message signatures. From an abstract class you can inherit the default implementations. For a good example take any listener say the WindowListener which is an interface. If you want to implement it you have to implement all the 7 methods that are there in the interface. Now look at the WindowAdapter. This is an abstract class which has implemented all the methods of the WindowListener but all the implementations are empty. To make use of this combination you will have to extend the WindowAdapter and only code that method you want.