Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between abstract class and interface [duplicate]

Possible Duplicate:
Interface vs Base class

I am not understanding the difference between an abstract class and an interface. When do I need to use which art of type?

like image 910
r.r Avatar asked Sep 03 '10 12:09

r.r


People also ask

What is the main difference between abstract class and interface?

Abstract Class Vs. Interface: Explore the Difference between Abstract Class and Interface in Java. The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.

Which is faster abstract and interface?

The performance of an abstract class is fast. The performance of interface is slow because it requires time to search actual method in the corresponding class. It is used to implement the core identity of class.

CAN interface have 2 abstract methods?

And in any case, the specification says a functional interface cannot have more than one abstract method. So an interface with two abstract methods cannot be used by specification.

Can abstract class replace interface?

To answer your question, yes you could use an abstract class (providing no implementation) instead of an interface but I'd consider this bad practice: You've used up your "one-shot" at inheritance (without gaining any benefit). You cannot inherit from multiple abstract classes but you can implement multiple interfaces.


3 Answers

Try thinking of it like this:

An abstract class creates an "is-a" relationship. Volkswagon is a Car.

An interface creates a "can-do" relationship. Fred can IDrive.

Moreover, Fred can IDrive, but Fred is a Person.

like image 187
Ragoczy Avatar answered Oct 18 '22 05:10

Ragoczy


When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

article along with the demo project discussed Interfaces versus Abstract classes.

like image 30
Pranay Rana Avatar answered Oct 18 '22 06:10

Pranay Rana


An abstract class is class probably with some abstract methods and some non-abstract methods. They do stuff (have associated code). If a new non-abstract class, subclasses the abstract class it must implement the abstract methods.

I.E.

public abstract class A {
    public string sayHi() { return "hi"; } // a method with code in it
    public abstract string sayHello();  // no implementation
}

public class B 
   : A
{
    // must implement, since it is not abstract
    public override string sayHello() { return "Hello from B"; }
}

Interface is more like a protocol. A list of methods that a class implementing that interface must have. But they don't do anything. They have just method prototypes.

public interface A
{
    string sayHi(); // no implementation (code) allowed
    string sayHello();  // no implementation (code) allowed
}

public class B
    : A
{
     // must implement both methods
    string sayHi() { return "hi"; }
    string sayHello() { return "hello"; }
}

Both are often confused because there is no protocol/interface in C++. So the way to simulate an interface behavior in that language is writing a pure virtual class (a class with only pure virtual functions).

class A {
    virtual int a() = 0;  // pure virtual function (no implementation)
} 
like image 26
Pablo Santa Cruz Avatar answered Oct 18 '22 05:10

Pablo Santa Cruz