Abstract vs Interface Class

Which is faster at class loading - interface or abstract class? Why?

Questions by murali.25

Showing Answers 1 - 36 of 36 Answers

poonamjava

  • Oct 21st, 2009
 

Abstrat class is a super class that only define a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.

Interface is a keyword to make a class fully abstract.

Poonam

  Was this answer useful?  Yes

DKD_Java

  • Oct 26th, 2009
 

In any case, the difference would be too small to be noticeable. The choice between choosing an Abstract class or an Interface is a design decision, not a performance one.

komara_2000

  • Oct 28th, 2009
 

1. Abstract class for tight coupling and interface for loose coupling.
2. Abstract classes does not support multiple inheritance but interfaces supports.
3. Abstract classes may have concrete methods and constructors but interfaces not.
4. Abstract classes take all the access specifiers but interfaces takes only public.
5. For design issues, avoid to use abstract classes like
ArrrayList al=new arrayList();//avoid to write
List l=new arrayList();//preferable

Note: Program to interfaces only not to implementations


Abstract class:
Abstract class must have at least one abstract method.
Abstract keyword must be used for all the methods.
Abstract class must have subclass.

Interface:
Inheritance is possible in interface.
Variables value must be defined in interface.
Many interface can be implemented by a single class.
Methods can be defined in interface and used in class.

Abstract class has both defined and undefined behaviour where as interface are used when any sub system of our application are frequently changed.

  Was this answer useful?  Yes

Mathewpjose

  • Nov 17th, 2009
 


Abstract classes are used when they have some sharable implementations and some specific methods left to subclasses to implement. Interface is used when there are no sharable implementation and the implementation changes frequently.

Abstract class can have methods which are abstract or not abstract. All methods of interface should be implemented unless the implementation is an abstract class.

Abstract class can be subclassed, interface class can be implemented.

Thanks
Mathew

Wrong question but yes, the design could very well decide how you are implementing though. Lets take a scenario where you have an abstract class vehicle and subclasses car and scooter. If numberplate and tires are attributes common for all vehicles and are inherited from the vehicle class or if you had an interface that forces you to specify the numberplate and tire attributes and their specific methods at the end, you will have the same classes with the same inherited attributes whether using abstract or interfaces. The memory footprint of the concrete classes whether using abstract classes or interfaces at the end is the same assuming they end up getting the same behavior and properties.

Intefaces provide loose coupling as is with for example all implementations of list interface. I can write methods that use all functionalities provided by list but can pass in any type of list tomorrow and re-use the same class. Abstract classes are better used in frameworks for providing some default implementation that you can reuse.

Abstract class:
Abstract class must have at least one abstract method.
Abstract keyword must be used for all the methods.
Abstract class must have subclass.

Interface:
Inheritance is possible in interface.
Variables value must be defined in interface.
Many interface can be implemented by a single class.
Methods can be defined in interface and used in class.

  Was this answer useful?  Yes

deshmukhcv

  • Jul 12th, 2010
 

I will give the Answer with Example :
Where to use Abstrace Class ::
Suppose I am having class Animal. and Dog Class and Cat Class. Animal Class having method eat(). In eat() I will write the common code which will be use for Dog Class and Cat Class. 

      Abstract Class Animal{
               abstaract run();
                public void eat(){    write the common code......     }
     }
     Class Dog{

                  public void eat() {

                          super.eat();

              }

                 // override run method and implement it.
     }

So in these case there is Reusability of code.
So Abstract Class Will use only in case of Reusability of Code.

While Interface will use in implemention of the abstract methods.

      
     

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions