0

Possible Duplicate:
Interface vs Base class
Interface vs Abstract Class (general OO)
Abstract class vs Interface in Java

So I am learning about interfaces and abstract classes in my programming course. The two topics seem very similar. I understand that they both are related to inheritance, but what is the difference between the two?

Community
  • 1
  • 1
  • 1
    start from here `http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo` – Jimmy Nov 08 '12 at 22:09

4 Answers4

1

An abstract class may contain code in method bodies, which is not allowed in an interface.

0

Interface has the methods that must be defined by the classes that implement it.

Abstract class is similar. However, in an abstract class, it can contain method definitions that the inheriting classes will automatically be able to invoke.

Bottom line: Use an interface if all implementing classes have completely different definitions for methods. Use an abstract class if the inheriting classes share some definitions and have different definitions for some methods.

Example:

public interface Shape {
    public float getArea();
    public void doThings();
}

public class Circle implements Shape{
    public float getArea() {
        return Math.PI * R * R;
    }

    public void doThings() { }
}

public abstract class Quadilateral implements Shape {

    public float getArea() { // defined
    return l * h;
    }

    public abstract void doThings(); // left abstract for inheriting classes
}

public class Rectangle extends Quadilateral {
    public void doThings() {
    // does things
    }
}

public class Parallelogram extends Quadilateral {
    public void doThings() {
    // does things
    }
}
rizalp1
  • 5,706
  • 2
  • 15
  • 19
0

Abstract classes can have method in them, while interfaces cannot have them.

I usually think of it like this:

  • If you want a class to force other classes to write methods, use an interface.
  • If you want a class to hold a common method for multiple classes, use an abstract class.

So for example, you might want to make an Animal class. And then have a Dog and Cat class. Both dogs and cats sleep. So you'd want them both to have a sleep method. And they sleep essentially the same way. So you'd want the Animal class to hold the method (since it's the same for Dog and Cat.

public abstract class Animal {
    public void sleep() {
        //make ZZZZ appear in the air or whatever
    }
}

public class Dog extends Animal {
    //This class automatically has the sleep method
}

public class Cat extends Animal {
    //This class automatically has the sleep method
}

On the other hand, say you had a Snake and Shark class. Both snakes and sharks attack, but in different ways (venom vs bitting). So you might have a Attacker interface.

public interface Attacker {
    //We HAVE to have this here
    public void attack(); //Note - we didn't write the method here, just declared
}

public Snake implements Attacker {
    //We HAVE to have this here
    public void attack() {
        //inject venom
    }
}

public Shark implements Attacker {
    public void attack() {
        //rip apart
    }
}

Note that abstract classes can also have non-defined methods (like an interface). So you could have public void makeMeanSound(); in Animal and then have the Dog growl and the Cat hiss. The main difference is that abstract classes can write actual methods.

Interface - force methods to be written (i.e. force Snake and Shark to have the attack() method).

Abstract class - provide common methods to subclasses.

Snowy Coder Girl
  • 5,162
  • 10
  • 36
  • 65
0

There are two main differences.

  1. Interfaces cannot contain any implementation. An interface is just a list of abstract methods, and all classes implementing that interface must implement all of those methods. An abstract class, on the other hand, can include implementations for some methods, which will then be usable by sub-classes.
  2. A class can only have one superclass, abstract or otherwise, but a class can implement any number of interfaces.
Aniket Schneider
  • 864
  • 1
  • 6
  • 21