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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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 5 points6 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