Pure Virtual Functions

Interfaces in Other Languages

What are the differences between interfaces in C++ and those in other programming languages like Java?

Abstract art representing computer programming

Interfaces in C++ are implemented using abstract classes with pure virtual functions, while languages like Java provide a dedicated interface keyword.

Here are some key differences between C++ and Java interfaces:

C++ Interfaces (Abstract Classes)

Syntax: In C++, interfaces are created using abstract classes with pure virtual functions.

class IShape {
public:
  virtual void Draw() = 0; 
};

Multiple Inheritance: C++ allows a class to implement multiple interfaces using multiple inheritance.

class Circle
: public IShape, public IOtherInterface {
public:
  void Draw() override {
    // Implementation
  }
};

Implementation: C++ interfaces can include non-pure virtual functions with implementations, providing default behavior.

class IShape {
public:
  virtual void Draw() = 0;
  virtual void Print() {
    std::cout << "Shape\n"; 
  }
};

No Interface Keyword: C++ does not have a specific keyword for interfaces; abstract classes serve this purpose.

Java Interfaces

Syntax: Java provides a specific interface keyword to define interfaces.

public interface Shape {
  void draw(); 
}

Single Inheritance with Implements: Java uses the implements keyword to indicate that a class implements an interface. Java supports single inheritance for classes but allows multiple interface implementations.

public class Circle
implements Shape, OtherInterface {
  public void draw() {
    // Implementation
  }
}

Default Methods: Java interfaces can include default methods with implementations, providing common functionality that can be overridden.

public interface Shape {
  void draw();
  default void print() {
    System.out.println("Shape"); 
  }
}

Specific Keyword: Java explicitly distinguishes interfaces from classes with the interface keyword, making it clear that the type is intended to be used as a contract.

Key Differences

  • Keyword Usage: Java uses a specific interface keyword, whereas C++ uses abstract classes with pure virtual functions.
  • Inheritance Model: C++ supports multiple inheritance directly, which allows a class to inherit from multiple abstract classes. Java restricts classes to single inheritance but allows multiple interfaces to be implemented.
  • Default Implementations: Both languages now support default method implementations in interfaces. However, the approach and syntax differ.
  • Abstract Classes vs Interfaces: In C++, abstract classes can have both pure virtual functions (acting like interface methods) and regular member functions, providing more flexibility in design. Java distinguishes between abstract classes (which can have member variables and concrete methods) and interfaces (which are meant to provide a strict contract with no state).

Example Comparison

Using an abstract IShape class in C++ looks like this:

#include <iostream>

class IShape {
public:
  virtual void Draw() = 0;
  virtual void Print() {
    std::cout << "Shape\n";
  }
};

class Circle : public IShape {
public:
  void Draw() override {
    std::cout << "Drawing Circle\n";
  }
};

Using a Shape interface in Java looks like this:

public interface Shape {
  void draw();
  default void print() {
    System.out.println("Shape");
  }
}

public class Circle implements Shape {
  public void draw() {
    System.out.println("Drawing Circle");
  }
}

Both examples achieve similar outcomes but use different syntax and concepts based on the language's design principles.

This Question is from the Lesson:

Pure Virtual Functions

Learn how to create interfaces and abstract classes using pure virtual functions

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Pure Virtual Functions

Learn how to create interfaces and abstract classes using pure virtual functions

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved