Abstraction in Java

  • Ways to achieve Abstraction
    • 1. Abstract Classes and Abstract Methods
    • 2. Interface
  • Advantage of Abstraction

Topic – 1 : Abstraction

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don’t know the internal processing about the message

An Abstraction is a process of exposing all the necessary details and hiding the rest. In Java, Data Abstraction is defined as the process of reducing the object to its essence so that only the necessary characteristics are exposed to the users.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java

  1. Abstract class (0 to 100%)
  2. Interface (100%)

In Java, we can achieve Data Abstraction using Abstract classes and interfaces.

  • Interfaces allow 100% abstraction (complete abstraction). Interfaces allow you to abstract the implementation completely.
  • Abstract classes allow 0 to 100% abstraction (partial to complete abstraction) because abstract classes can contain concrete methods that have the implementation which results in a partial abstraction.

 

Abstract Class

  • A class which is declared as abstract is known as an abstract class.
  • An Abstract class is created through the use of the abstract keyword.
  • It is used to represent a
  • Object Can not be created of an Abstract
  • Does not Support multiple inheritance.

Points to Remember

  • An abstract class must be declared with an abstract
  • It can have abstract and non-abstract methods.
  • The class has to be declared as abstract if it contains at least one abstract
  • It can have constructors, static methods and final methods.
  • It can have any type of variables (Static, non-static, final, non-final etc)

Syntax of declaring an abstract class:

  • To declare an abstract class we use the keyword The syntax is given below:

access-specifier abstract class ClassName

{

//class body

}

Abstract Methods in Java

•   A method which is declared as abstract and does not have implementation is known as an abstract method.

  • The declaration of an abstract method must end with a semicolon ;

•   The child classes which inherit the abstract class must provide the implementation of these inherited abstract methods.

  • Syntax of declaring abstract methods:

access-specifier abstract return-type method_name();

  • Example:

public abstract void printStatus();

 

Example – 1

public void move()

{

System.out.println(“Car moves faster.”);

}

public class Car extends Vehicle {
public abstract class Vehicle { public abstract void move();

}

An abstract class that has abstract method

 

Example – 2
An abstract class that has abstract , non-abstract methods and Constructor
public abstract class Vehicle { public class Car extends Vehicle {
public Vehicle()

{

System.out.println(“Vehicle is Created.”);

}

public void move()

{

System.out.println(“Car moves faster.”);

}

public abstract void move();

 

public void carry()

{

System.out.println(“All Vehicle carry loads”);

}

public static void main(String[] args) { Car c1 = new Car();

c1.move();

c1.carry();

}

}

} // Outputs:

Vehicle is Created. Car moves faster.

All Vehicle carry loads

 

Interface in Java

  • An interface is a blueprint or template of a class.
  • It is much similar to the class but the only difference is that it has abstract methods and

static constants.

  • Object can not be created of an
  • All the methods in an interface are by default abstract.
  • All variables are declared as both static and
  • An interface does not contain any constructors.
  • An interface can not be extended or inherited by a class; it is implemented by a
  • Multiple-Inheritance supported.

 

Internal addition by the compiler

Syntax of declaring an Interface

interface <interface_name>{

// declare constant fields

// declare methods that abstract

//by default.

}

The relationship between classes and interfaces

As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface

Example – 1

public class SIBL implements Bank{ public double account ()

{

System.out.println(“Account System in SIBL”);

}

}

class TestInterface{

public static void main(String[] args){ SIBL b=new SIBL();

b. account ();

 

DBBL d=new DBBL();

d. account ();

}

}

interface Bank{ double account();

}

 

public class DBBL implements Bank{ public double account ()

{

System.out.println(“Account System in DBBL”);

}

}

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as

multiple inheritance

Example – 2

interface FirstInterface { public void myMethod();

}

class DemoClass implements FirstInterface, SecondInterface {

public void myMethod() {

System.out.println(“Some text..”);

}

interface SecondInterface {

public void myOtherMethod();

}

 

 

Thank you!

 

By Md Jakaria Nur

Software Engineer

Leave a Reply

Your email address will not be published. Required fields are marked *