0

I know the difference between "interface" and "abstract class". But could you provide me the single example which can be built through the "interface" but not through the "abstract class" leaving the example of "multiple inheritance"?

3 Answers3

1

A simple example is with objects that represent the same entity but have different bevaviour. Consider for instance the birds. Some birds can fly and some can't. It would be wrong to define an abstract class Bird which forces all it's subclasses to implement a fly method.

So in such a case it's ok to define methods as eat() or sleep() as abstract in the abstract class Bird but not fly() since not all birds can fly.

Generally you would define an interface called for instace Flyable which would contain the definition of the fly() method, which would have to be overriden by classes implementing the interface.

In the end you would end up with something like:

public abstract class Bird{
  public abstract void eat();
  public abstract void sleep();
}

public interface Flyable{
  void fly();
}

public class Eagle extends Bird implements Flyable{ 
 .... has to implement eat(), sleep() and fly()
}

public class Ostrich extends Bird{
... has to implement only eat() and sleep() since ostrich can't fly
}
MihaiC
  • 1,618
  • 1
  • 10
  • 14
0

100% percentage abstraction is called Interface.

List is a interface. It jus jas method definition.

Abstract Class : It has some method implementation.

AbstractList is a abstract class . It has method implementation.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
  • 2
    "100% percentage abstraction is called Interface." not true with [Java 8 default methods](http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html) – Florent Bayle Dec 18 '14 at 11:36
  • @Regent There are some differences, you can have a look at http://stackoverflow.com/q/19998454/1820501 and http://stackoverflow.com/q/20139404/1820501 – Florent Bayle Dec 18 '14 at 11:57
  • @FlorentBayle well, yes, it all ends up in difference between interface and abstract class. And usage of default method without state (fields). The most interesting thing is that you can't implement two interfaces if they both have default same method. Yes, it is logical (same story as with extending two classes), but it will cause troubles for someone who uses `default` in future, that's for sure. – Regent Dec 18 '14 at 12:20
0

Comparable interface in java.lang.Comparable can never be implemented as a class (i.e Abstract class) This is because the compareTo method depends completely upon the class which imnplements it. And it is required that for all object comparisons, they should have implemented this method (The objects that are being compared)

Note: Comparable class is used to compare 2 objects for equality

Whereas AbstractStringBuilder is better written as an abstract class is extended by both StringBuilder as well as StringBuffer. This way the common functionalities (like charAt, expandCapacity, indexOf )in both the classes can be put in this abstract class..This way code can be reused which cannot be in case of an interface

FatherMathew
  • 960
  • 12
  • 15