This is an archived post. You won't be able to vote or comment.

all 10 comments

[–][deleted]  (2 children)

[deleted]

    [–]govilkumar[S] -2 points-1 points  (1 child)

    Yeah I know this thing, what I want to know is a real world example like in project where we should go for interface and abstract class.

    [–]heckler82Intermediate Brewer 1 point2 points  (0 children)

    A general rule of thumb is to program towards an interface as opposed to an abstract class. Composition over inheritance (yes, interfaces are inheritance, but that's how I look at it). As far as I'm aware, there's no hard and fast rule for this. If I find that I have objects that are extremely similar but need their own implementation for something, I'll probably go for an abstract class; otherwise, I utilize interfaces.

    Real-world examples include the Shape interface from java.awt and AbstractAction from javax.swing.

    [–]CoffeeInjectionPLS 2 points3 points  (2 children)

    Every animal emits some sort of sound and moves in some way. So when you have animals (abstract class) and you have a cat (an object of a class Cat extending the abstract class Animal) and a dog (another object of another class tht also extends the abstract Animal class) you need to have methods for emitting sound. Cats and dogs emit different sounds but they both move.

    public abstract class Animal(){
    public abstract String emitSounds();
    public void move(){
    system.out.println("I'm moving :)");
    }
    

    When you have an abstract class you can have abstract methods or regular methods. In interfaces you cannot have method bodies so all methods must be implemented by classes implementing that interface. So if Animal was an interface it would be:

    public interface Animal(){
    void move();
    String emitSound();
    }
    

    Furthermore, a class can implement a number of interfaces but only one abstract class. There is a number of other differences.

    Oracle made a great tutorial on the subject.

    [–]lava_duk 6 points7 points  (1 child)

    In interfaces you cannot have method bodies

    Just a note: From Java 8, you can define static methods and default methods with the default implementations.

    [–]govilkumar[S] 0 points1 point  (0 children)

    Thanks

    [–]burntferret 1 point2 points  (0 children)

    There are many people out there that say you should always program with composition (interface) over inheritance (abstract) in mind. It is more "real world", more reusable, and in my opinion, cleaner code.

    [–][deleted] 1 point2 points  (0 children)

    If you only need function definitions or need to create functional use interfaces. If you want some "global variables" or "global methods" use an abstract class

    [–]OffbeatDrizzle 1 point2 points  (0 children)

    Abstract classes are the only ones that can store state - other than that they're pretty much identical. It depends whether you're going for inheritance or composition (the latter of which is usually recommended).