Java14: Abstraction

Abstraction exposed only the necessary functionality to the outside world without revealing the details about how it works. Programmers can work on complex logic without worrying about its implementation. In other words, it only reveals what it does rather than how it does it.

The abstraction concept is realized by using abstract class and interface.

Abstract Class

An abstract class may have at least one or more abstract methods. It also may have concrete methods(the normal methods).

Abstract method

An abstract method is a method that does not have implementation. The abstract method only has a definition. Its definition is implemented by its child class.

modifier abstract returnType methodName(parameters);

An abstract method is defined using an abstract keyword. It does not have a method body so it ends with a semicolon.

abstract class Animal{
    public abstract void talk();
}

The abstract class is defined using the abstract keyword. the abstract method is implemented by its child class through inheritance. For example, the Bird class inherits the abstract Animal class, so the Bird class must implement the abstract talk() method.

The UML diagram shows how an abstract class Animal is extended by the Bird class. The following code sample shows the Bird class implementing the talk() method.

class Bird extends Animal{
    public void talk(){
        System.out.println("Chirp");
    }
}

We can't create an object using an abstract class. But this doesn't mean we can't define a constructor in an abstract class. A constructor defined is the abstract class is called by its subclass. Even though we can't instantiate an abstract class, it can be used as a data type as follows:

class Demo{
    public static void main(String[] args){
        Animal anAnimal = new Bird();
        anAnimal.talk();
    }
}

Interface

Unlike abstract class, an interface is a pure abstraction. This is because an interface may only contain abstract methods and constants.

The UML diagram shows an archetype <> is used to differentiate it from the abstract class.

interface Animal{
    void talk();
}

It is implicit that any method declared in an interface is abstract and public, so we do not have to use abstract and public keywords to define them.

A class that implements an interface uses the implements keyword. For example, the Bird class implements the Animal Interface. The Bird class must implement the talk() method.

In UML, implementation is represented using a dashed line and unfilled triangle that points to an interface. Following is the code sample:

class Bird implements Animal{
    public void talk(){
        System.out.println("Chirp");
    }
}

A class that implements an interface must implement all the abstract classes in the interface. Anyhow if at least one or more methods of the interface didn't implement, the class should be defined as an abstract class.

An interface can't implement another interface but an interface can extend another interface using extends keyword.

interface B extends A{
    // codes
}

A class may implement more than one interface at a time. Assume we have two interfaces called A and B. To implement these interfaces, we just list the interfaces separated by a comma.

class C implements A, B{
    // codes
}

A class can extend as well as implement one or more than one interfaces. Imagine we have an interface called A and a superclass called B. The following example shows a class called C extends superclass B and implements interface B.

class C extends B implements A{
    // codes
}

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net