In this note, you will quickly know about the Abstract Classes and Interfaces.

Abstraction

The abstract keyword is a non-access modifier, used for classes and methods:

  • Abstract class: a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
    • cannot instantiate an abstract class using the new operator
    • constructor can be defined, which are invoked in the constructors of its subclasses
    • An abstract class can contain regular methods and abstract methods.
    • A subclass can be abstract even if its superclass is concrete.
  • Abstract method: can only be used in an abstract class, and it does not have a body (no {}). The body is provided by the subclass (inherited from).
    • non-abstract subclass extended from an abstract class must have all the abstract methods implemented (even if they are not used in the subclass)
    • subclass can override a abstract method from its abstract superclass
    • If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract as well.

A concrete class is a class that has an implementation for all of its methods.

Example - cannot instantiate an abstract class using the new operator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
abstract class GeometricObject { 
protected GeometricObject() {
//...
}
// Some Code...
}

class Circle extends GeometricObject {
public Circle() {
//...
}
// Some Code...
}

class Rectangle extends GeometricObject {
public Rectangle() {
//...
}
// Some Code...
}

class MyMainClass {
public static void main(String[] args) {
//Wrong Example:
//GeometricObject gObject1 = new GeometricObject() // Error
// cannot instantiate an abstract class using the `new` operator

//Correct Example:
GeometricObject gObject1 = new Circle(); // OK
GeometricObject gObject2 = new Rectangle(); // OK

//an abstract class can be used as a data type
//here: an array whose elements are of GeometricObject type
GeometricObject[] objects = new GeometricObject[10]; //OK
objects[0] = new Circle(); //OK
}
}

Example - Abstract Class and Abstract Method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Abstract class
abstract class Animal {

// Abstract method (does not have a body)
public abstract void animalSound();

// Regular method
public void sleep() {
System.out.println("Zzz");
}

}

// Subclass (inherit from Animal)
class Pig extends Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
}

class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}

//Output:
//The pig says: wee wee
//Zzz

Interface

An interface is a completely “abstract class” that contains only constants and abstract methods.

In many ways, an interface is similar to an abstract class, but the intent of an interface is to specify behavior for objects.

  • Like abstract classes, interfaces cannot be used to create objects
    • cannot instantiate an abstract class using the new operator
  • Interface has no constructors
  • To access the interface methods, the interface must be “implemented” with the implements keyword
    • the idea is like inherited but you use implements keyword instead of extends
  • You can use an interface as a data type for a variable, as the result of casting

In an interface:

  • All data fields are public static final
  • All methods are public abstract

If you omitted the modifiers in Interfaces, the compiler will automatically run as above modifiers.

In rare occasions, a class may implement two interfaces with conflict information (e.g., two same constants with different values or two methods with same signature but different return type).

This type of errors will be detected by the compiler.

Example - Interface Declaration:

1
2
3
4
5
6
7
// interface
interface Animal {
//Interface can only contain constants and abstract methods
public static final int a = 1;
public void animalSound(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)
}

Example: Access the interface methods using implements keyword:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}

// Pig "implements" the Animal interface
class Pig implements Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
public void sleep() {
// The body of sleep() is provided here
System.out.println("Zzz");
}
}

class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}

//Output:
//The pig says: wee wee
//Zzz

Java does not support “multiple inheritance” (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces.

Example- Multiple Implement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
interface FirstInterface {
public void myMethod(); // interface method
}

interface SecondInterface {
public void myOtherMethod(); // interface method
}

class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("Some text..");
}
public void myOtherMethod() {
System.out.println("Some other text...");
}
}

class MyMainClass {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}

Interfaces vs. Abstract Classes

  • All classes share a single root, the Object class, but there is no single root for interfaces.

  • If a class extends an interface, this interface plays the same role as a superclass.

  • interface can be used as a data type and cast a variable of an interface type to its subclass, and vice versa

When to Use Interface or Class?

Abstract classes and interfaces can both be used to model common features.

When to Use Class inheritance

  • a strong is-a relationship can be modeled using inheritance.
    • i.e. x is a y

Example:

Teacher is a person.

So the Teacher class extends the Person (abstract) class.

When to Use Interface

  • A weak is-a relationship can be modeled using interfaces.
    • i.e. x is kind of y

Example1:

ArrayList, LinkedList, Vector, Stack are both kind of Lists.

So the ArrayList, LinkedList, Vector, Stack classes implements the List interface.

Example2:

Pig, Cat, Dog are both kind of Animals.

So the Pig, Cat, Dog classes implements the Animal interface.

Referenece

Java Interface

Java Abstraction

Concrete class in Java