-4

We can achieve everything with interface,For eg- it solves our main problem of multiple inheritance, we can achieve 100 % abstraction. then what is the need to use an abstract class

See I have already read that abstract class can be extended and used by the closely related classes For eg: Circle Extends Shape etc.

I also read that an interface can be used for adding a capability to the class like implements Comparable, Runnable ,Serializable etc ..

So since we can do everything with interface (even the things that an abstract class can do), then what was the need for abstract classes.

Also what is the use of a non abstract method in an abstract class.

Anshul
  • 415
  • 4
  • 15

2 Answers2

1

Abstract class allows you to define fields so it's easy to implement some stateful base implementation.

In pre-Java 8 there were no default methods in interfaces, so the only way how to provide default method implementation was to create abstract class.

Since Java 8, you can define at interface default methods and static methods, but still there are no fields.

what is the use of a non abstract method in an abstract class

Methods that are not marked as abstract can be used as standard methods in standard class. The reason why you have non-abstract and abstract methods is, that some class functionality (some method) may depend on abstract method. Here is an example:

public class AbstractGreeting {
    private final String name;
    protected AbstractGreeting(String name) {this.name = name;}
    public void displayGreeting() {System.out.println(getGreeting() + " " + name + "!");}        
    protected abstract String getGreeting();
}

Some concret implementations:

public class HelloGreeting extends AbstractGreeting {
    public HelloGreeting(String name) { super(name);}
    @Override protected String getGreeting() { return "Hello"; }
}

public class AlohaGreeting extends AbstractGreeting {
    public AlohaGreeting (String name) { super(name);}
    @Override protected String getGreeting() { return "Aloha"; }
}

And usage:

HelloGreeting hg = new HelloGreeting("World"); hg.displayGreeting();
AlohaGreeting ag= new AlohaGreeting ("World"); hg.displayGreeting();

Sure, abstract classes can be transformed into the interface when you force implementator to return object's state. For example abstract class above can be rewritten as:

public interface Greeting {
    String getName();
    String getGreeting();
    default void displayGreeting() {System.out.println(getGreeting() + " " + name + "!");}
}

Implementation:

public class HelloGreeting implements Greeting {
     @Override public String getName() {return "World";}
     @Override public String getGreeting() {return "Hello";}
}

But sometimes, it is better to not provide access to object's state, therefore abstract classes might be in some situation more suitable.

matoni
  • 2,479
  • 21
  • 39
0

I think there are a lot of duplicated questions like this, I found this. A few quotes from e-satis:

Abstract classes, unlike interfaces, are classes. They are more expensive to use, because there is a look-up to do when you inherit from them. Abstract classes look a lot like interfaces, but they have something more: You can define a behavior for them. It's more about a guy saying, "these classes should look like that, and they have that in common, so fill in the blanks!".

In short, the difference is interface and abstract class are different levels of abstraction. interface is more abstract, because it only defines what methods you have to implement like function signature in C. abstract class, however, will further define the behaviors of functions. In addition, you can define fields in abstract class, while you cannot do so in interface.

Peter Paul
  • 384
  • 3
  • 13