Apex Interfaces & Abstract Classes

Apex Interfaces & Abstract Classes

In Apex, interfaces and abstract classes are powerful tools used to define rules and shared structures for other classes to follow. They are widely used in real-time business logic, where multiple classes need to implement common behavior, but in their own specific way. 

Both interfaces and abstract classes help with polymorphism, code reusability, and making your application easier to maintain. 

Feature Interface Abstract Class
Can have method body No (only method signatures) Yes (can have code + abstract methods)
Inheritance keyword implements extends
Multiple inheritance Allowed Not allowed in Apex

Interface in Apex

An interface is like a contract. Any class that implements an interface must provide its own version of the methods declared in the interface. 

Syntax: 

public interface MyInterface {
void doSomething(); // No body
}

Implementation in Class:

public class MyClass implements MyInterface {

public void doSomething() {
System.debug(‘Doing something from MyClass’);
}

}

Abstract Class in Apex

An abstract class is a class that cannot be instantiated directly. It may contain both:
• Abstract methods (without body),
• Concrete methods (with actual code).
 It acts as a template for other classes. Syntax:

public abstract class Animal {

public abstract void makeSound(); // No body

public void eat() {
System.debug(‘Animal is eating’);
}

 

Implementation in Child Class:

public class Dog extends Animal {

public void makeSound() {
System.debug(‘Dog barks’);
}

}

Code Example

1: Interface Implementation

public interface Flyable {
void fly();
}

public class Bird implements Flyable {
public void fly() {
System.debug(‘Bird is flying’);
}
}

Bird b = new Bird();
b.fly();

Code Explanation:

• Flyable is an interface.
• Bird implements Flyable and gives its own version of fly().
• The object b calls the method fly() from Bird.

Output:

Bird is Flying

2: Abstract Class

public interface Flyable {
void fly();
}

public class Bird implements Flyable {
public void fly() {
System.debug(‘Bird is flying’);
}
}

Bird b = new Bird();
b.fly();

Code Explaination

Shape is an abstract class with one abstract method area() and one concrete method describe().
Rectangle extends Shape and defines the area() method.
The object r can access both describe() (from parent) and area() (from child).

Output:

Calculating area of a shape
Area = length × width

Real-Life Analogy

Interface → Job Description 

An interface is like a job description that says: 

        “Whoever takes this job must do these tasks.” 

It doesn’t say how to do it, only what must be done. 

Abstract Class → Tool Template 

An abstract class is like a partially-built machine. 

       It has some working parts already, and you can plug in your own parts to make it        

       Complete. 

Tasks For Practice

Task 1: Create an interface Playable with method play(). Create two classes Music and Video that implement the interface and define their own version of play().

Task 2: Create an abstract class Appliance with a method turnOn() and abstract method functionality(). Create a class WashingMachine that extends Appliance and implements functionality().

Task 3: Create an interface Shape with method draw(). Then create classes Circle and Square that implement Shape and provide their own draw() method.

Task 4: Create an abstract class Person with a method greet() and an abstract method work(). Create a class Teacher that extends Person and provides its own work() method.

Task 5: Create an interface Printable with a method printData(). Create a class Invoice that implements this interface and prints "Invoice printed successfully" when printData() is called.

Task 6: Create an interface AnimalSound with a method makeSound(). Implement it in two classes: Cat and Cow.

• Cat → prints “Meow”
• Cow → prints “Moo” Create and call the method for each class.

Task 7: Create an abstract class Vehicle with:

 • A concrete method displayType() that prints “This is a vehicle”
 • An abstract method startEngine()

Create a class Car that extends Vehicle, overrides startEngine() to print “Engine started.” 
Call both methods.