Saturday, December 3, 2011

Abstract classes vs. interfaces

  • Interface vs. abstract class
Choosing interfaces and abstract classes is not an either/or proposition. If you need to change your design, make it an interface. However, you may have abstract classes that provide some default behavior. Abstract classes are excellent candidates inside of application frameworks


  • When to use Abstract Class-If we have a base class where all the classes will perform the same function, then we can define that in our Abstract class. If you plan on updating this base class throughout the life of your program, it is best to allow that base class to be an abstract class. Because you can make a change to it and all of the inheriting classes will now have this new functionality

  • When To use interface – Interfaces are useful when you do not want classes to inherit from unrelated classes just to get the required functionality.It is used where there of chances adding new method in future. Interfaces are more used to set standards

When should I use abstract classes and when should I use interfaces?

  • Use Interfaces when…
You see that something in your design will change frequently.
If various implementations only share method signatures then it is better to use Interfaces.
you need some classes to use some methods which you don't want to be included in the class, then you go for the interface, which makes it easy to just implement and make use of the methods defined in the interface.


  • Use Abstract Class when…
If various implementations are of the same kind and use common behavior or status then abstract class is better to use.
When you want to provide a generalized form of abstraction and leave the implementation task with the inheriting subclass.
Abstract classes are an excellent way to create planned inheritance hierarchies. They're also a good choice for nonleaf classes in class hierarchies.





  • What are the differences between Interface and Abstract class?

In case of abstract class, a class may extend only one abstract class. A Class may implement several interfaces.
An abstract class can have non-abstract methods. All methods of an Interface are abstract.
An abstract class can have instance variables. An Interface cannot have instance variables.
An abstract class can have any visibility: public, private, protected. An Interface visibility must be public (or) none.
An abstract class can contain constructors . An Interface cannot contain constructors

  • When you declare a method as abstract, can other nonabstract methods access it?
Yes, other nonabstract methods can access a method that you declare as abstract.



http://www.developersbook.com/corejava/interview-questions/corejava-interview-questions-faqs.php
http://www.techartifact.com/blogs/2009/08/interface-vs-abstract-class.html





  • If you need to change your design, make it an interface

Abstract classes are excellent candidates inside of application frameworks.
Abstract classes let you define some behaviors; they force your subclasses to provide others. For example, if you have an application framework, an abstract class may provide default services such as event and message handling. Those services allow your application to plug in to your application framework. However, there is some application-specific functionality that only your application can perform. Such functionality might include startup and shutdown tasks, which are often application-dependent. So instead of trying to define that behavior itself, the abstract base class can declare abstract shutdown and startup methods. The base class knows that it needs those methods, but an abstract class lets your class admit that it doesn't know how to perform those actions; it only knows that it must initiate the actions. When it is time to start up, the abstract class can call the startup method. When the base class calls this method, Java calls the method defined by the child class.

Many developers forget that a class that defines an abstract method can call that method as well. Abstract classes are an excellent way to create planned inheritance hierarchies. They're also a good choice for nonleaf classes in class hierarchies

http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html




  • What is an abstract class, and when should it be used?

http://www.javacoffeebreak.com/faq/faq0084.html



  • What is the difference between interface and abstract class ?

INTERFACE
1.all methods are abstract.
2. does not use abstract keyword before abstract method.

ABSTRACT
1. ATLEAST ONE METHOD IS ABSTRACT.
2. uses the abstract keyword before abstract method.
http://www.geekinterview.com/question_details/14188




  • Write down three differences between abstract classes and interfaces in Java.


A class can implement any number of interfaces but can subclass at most one abstract class.
An abstract class can have nonabstract methods; all the methods of an interface are effectively abstract.
An abstract class can declare and use fields; an interface cannot,although it can create static final constants.
An abstract class can have methods whose access is public, protected, private, or none (package). An interface’s methods are implicitly public.
An abstract class can define constructors; an interface cannot.




  • Abstract Classes versus Interfaces

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods.
Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead

By comparison, abstract classes are most commonly subclassed to share pieces of implementation.
A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).
http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html




  • Java interface versus abstract class


An interface differs from an abstract class because an interface is not a class.
An interface is essentially a type that can be satisfied by any class that implements the interface.

Abstract classes are meant to be inherited from, and when one class inherits from another it means that there is a strong relationship between the 2 classes.
With an interface on the other hand, the relationship between the interface itself and the class implementing the interface is not necessarily strong

Java does not allow multiple inheritance
a class can implement multiple interfaces
which could be considered as an alternative to for multiple inheritance.
So, one major difference is that a Java class can inherit from only one abstract class, but can implement multiple interfaces


When to use abstract class and interface in Java

    An abstract class is good if you think you will plan on using inheritance since it provides a common base class implementation to derived classes.
    An abstract class is also good if you want to be able to declare non-public members. In an interface, all methods must be public.
    If you think you will need to add methods in the future, then an abstract class is a better choice. Because if you add new method headings to an interface, then all of the classes that already implement that interface will have to be changed to implement the new methods. That can be quite a hassle.
    Interfaces are a good choice when you think that the API will not change for a while.
    Interfaces are also good when you want to have something similar to multiple inheritance, since you can implement multiple interfaces.
http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/




No comments:

Post a Comment