Week 28 — What is the difference between abstract classes and interfaces in Java?

Question of the Week #28
What is the difference between abstract classes and interfaces in Java?
8 Replies
Eric McIntyre
Eric McIntyre2y ago
Classes contain actual behavior and state while interfaces typically just contain information what classes implementing the interface can do. Interfaces can be seen as a contract or specification. By implementing an interface, the class must provide all methods specified in the interface.
interface SomeInterface{
void someMethod();//no implementation here
}
class SomeClass implements SomeInterface{
@Override
public void someMethod(){
//this method must be present because SomeClass implements SomeInterface
}
}
interface SomeInterface{
void someMethod();//no implementation here
}
class SomeClass implements SomeInterface{
@Override
public void someMethod(){
//this method must be present because SomeClass implements SomeInterface
}
}
On the other hand, abstract classes are typically incomplete classes. They can contain state and behaviour but some implementation could be missing. As with interfaces, concrete (non-abstract) classes need to provide an implementation of all abstract methods from parent classes:
public abstract class SomeAbstractClass{
private int n=10;
public void doSomethingNTimes(){
for(int i=0;i<n;i++){
doSomething();
}
}
abstract void doSomething();//no implementation here
}
class SomeConcreteClass extends SomeAbstractClass{
@Override
void doSomething(){//method must be present
System.out.println("I am doing something");
}
}
public abstract class SomeAbstractClass{
private int n=10;
public void doSomethingNTimes(){
for(int i=0;i<n;i++){
doSomething();
}
}
abstract void doSomething();//no implementation here
}
class SomeConcreteClass extends SomeAbstractClass{
@Override
void doSomething(){//method must be present
System.out.println("I am doing something");
}
}
Java does not allow a class to extend multiple other classes. As such, it is not possible to create a class extending two abstract classes. However, any class can implement as many interfaces as it likes:
abstract class SomeAbstractClass {}
abstract class OtherAbstractClass {}
interface SomeInterface {}
interface OtherInterface {}

//class IllegalClass extends SomeAbstractClass, OtherAbstractClass {}//compiler error
class ValidClass extends SomeAbstractClass implements SomeInterface, OtherInterface {}//allowed, classes can implement as many interfaces as wanted

interface YetAnotherInterface extends SomeInterface, OtherInterface {}//interfaces can also extend (multiple) other interfaces but no classes
abstract class SomeAbstractClass {}
abstract class OtherAbstractClass {}
interface SomeInterface {}
interface OtherInterface {}

//class IllegalClass extends SomeAbstractClass, OtherAbstractClass {}//compiler error
class ValidClass extends SomeAbstractClass implements SomeInterface, OtherInterface {}//allowed, classes can implement as many interfaces as wanted

interface YetAnotherInterface extends SomeInterface, OtherInterface {}//interfaces can also extend (multiple) other interfaces but no classes
⭐ Submission from dan1st#0000
Eric McIntyre
Eric McIntyre2y ago
Interface cannot have method implementations but abstract classes can have methods with implementation Abstract class can have public and private both, but interface just public You can make a class inherit an other class but not the interface
Submission from thehsb#0000
Eric McIntyre
Eric McIntyre2y ago
The difference between an abstract class and an interface is that you can implement multiple interfaces but you can only extend one superclass. Interfaces allow you to define behavior for classes or objects that are different. Abstract classes are better for when you have objects that are similar
Submission from Chowder#5643
Eric McIntyre
Eric McIntyre2y ago
The difference between an abstract class and an interface is how in an interface, everything is final. Also they are called differently, One uses extend, which can inly be used once per class, and one uses implements, which can be used multiple times. An interface is more like a contract, where you put methods in that you require the user to define, while in an abstract class you can define them and the classes can inherit that method.
public interface InterfaceExample{
void sayHello();
}
public abstract class AbstractClassExample{
String hello = “Hello World!”;
public String getWord(){
return “test 2”;
}
}
public class Main implements InterfaceExample extends AbstractClassExample{
void sayHello(){ System.out.println(hello);}
public static void main(String args[]){
Main man = new Main();
man.sayHello();
System.out.println(man.getWord());
}
}
public interface InterfaceExample{
void sayHello();
}
public abstract class AbstractClassExample{
String hello = “Hello World!”;
public String getWord(){
return “test 2”;
}
}
public class Main implements InterfaceExample extends AbstractClassExample{
void sayHello(){ System.out.println(hello);}
public static void main(String args[]){
Main man = new Main();
man.sayHello();
System.out.println(man.getWord());
}
}
Output:
Hello world!
test 2
Hello world!
test 2
Submission from mochatitan#0000
Eric McIntyre
Eric McIntyre2y ago
Most obviously, the difference between an abstract class and an interface is functionally in that an implementing class can only “extend” one class, whereas it can “implement” any number of interfaces. Also, an abstract class can define and use instance variables whereas an interface cannot. Moreover, an abstract class can extend other abstract classes as well as implement any number of interfaces, whereas an interface can only extend other interfaces. More importantly though, their purpose is also different. An abstract class is used to define “what” the the object is, whereas an interface defines the object’s “traits”. For instance, a class “Cat” may extend an abstract class “Animal” and implement the interface “Tamable” which might define the method “tame()”. In this case, we could say “Cat is an animal, which has the trait of being tamable”. Although you might consider refactoring that so that there is instead an abstract class Pet which implements Tamable and extends Animal, and then Cat can extend Pet. It’s important to structure your inheritance with reusability in mind, since that’s kind of the whole point.
Submission from justisr#0000
Eric McIntyre
Eric McIntyre2y ago
Abstract classes in Java allow you to create classes that can contain both regular and abstract methods. When a class extends an abstract class, it must implement the abstract methods defined in the abstract class. Abstract methods are declared without a body and are meant to be overridden and implemented by the subclasses. On the other hand, interfaces in Java are used to define a contract that a class can implement. Interfaces only contain abstract method declarations, and you don’t need to use the ‘abstract’ keyword when declaring these methods. A class that wants to use the functionality defined in an interface must implement the interface using the ‘implements’ keyword.
Submission from xgpq#0000
Eric McIntyre
Eric McIntyre2y ago
Interfaces are more of a building plan for a class. Interfaces can't declare instance fields, constructors, they can only define methods. Most of the time, those methods won't have an implementation in the interface itself, but interfaces can provide their own, default implementation with the aforementioned modifier on the method. Interfaces are often used to abstract away some class members, and make them accessible from the interface itself, to help with OOP. Abstract classes are a bit more advanced. They're regular classes, can do everything a normal class can, but they can define abstract methods, that don't have an implementation on their own. Similarly to an interface, child classes have to provide their own implementation of those abstract methods, or mark themselves abstract as well. If you want to instantiate an abstract class, you have to implement the abstract members in an anonymous inner class, same with interfaces.
Eric McIntyre
Eric McIntyre2y ago
Interface-style implementation:
interface Greeter {
String sayHello(); // abstract method definition: we say it exists, but don't provide code
default String greet(String name) { return sayHello() + ", " + name; }
}
class HelloGreeter implements Greeter {
@Override
public String sayHello() { return "Hello"; }
}
class HiGreeter implements Greeter {
@Override
public String sayHello() { return "Hi"; }
}
Greeter hello = new HelloGreeter();
Greeter hi = new HiGreeter();
System.out.println(hello.greet("Alice")); // Hello, Alice
System.out.println(hi.greet("Bob")); // Hi, Bob
System.out.println(hello.sayHello()); // Hello
interface Greeter {
String sayHello(); // abstract method definition: we say it exists, but don't provide code
default String greet(String name) { return sayHello() + ", " + name; }
}
class HelloGreeter implements Greeter {
@Override
public String sayHello() { return "Hello"; }
}
class HiGreeter implements Greeter {
@Override
public String sayHello() { return "Hi"; }
}
Greeter hello = new HelloGreeter();
Greeter hi = new HiGreeter();
System.out.println(hello.greet("Alice")); // Hello, Alice
System.out.println(hi.greet("Bob")); // Hi, Bob
System.out.println(hello.sayHello()); // Hello
Abstract classes:
abstract class Greeter {
public abstract String sayHello();
public String greet(String name) {
return sayHello() + ", " + name;
}
}
class HelloGreeter extends Greeter {
@Override
public String sayHello() { return "Hello"; }
}
class HiGreeter extends Greeter {
@Override
public String sayHello() { return "Hi"; }
}
// the rest is the same
abstract class Greeter {
public abstract String sayHello();
public String greet(String name) {
return sayHello() + ", " + name;
}
}
class HelloGreeter extends Greeter {
@Override
public String sayHello() { return "Hello"; }
}
class HiGreeter extends Greeter {
@Override
public String sayHello() { return "Hi"; }
}
// the rest is the same
⭐ Submission from 0x150#0000
Want results from more Discord servers?
Add your server