Notice

╠ This is my personal blog and my posts here have nothing to do with my employers or any other association I may have. It is my personal blog for my personal experience, ideas and notes. ╣

Wednesday, May 20, 2020

Abstract Data Type

What is the data type?

A data type is an attribute in a programming language which define the domain of values and operations on that domain.

  • Define a certain domain of value. 
  • Define operations allowed on those values. 

Example 

int type
  • Take only integer values
  • Operations supported: addition, subtraction, multiplication, bitwise operation etc.  
float type 
  • Take only floating values 
  • Operations supported: addition, subtraction, multiplication, etc.  (it does not support operations like bitwise and modulus). 


Now, what is the user-defined data type? 

A user data type is NOT an attribute in a programming language but defines by the user which define the domain of values.

In below example, we define the user-defined data type using class. Here Person class is the type which is consist of bunch of String, LocalDate and Address fields. Person class have a user define a data type of Address.

Example

public class Person {
    private String firstName;
    private String lastName;
    private LocalDate dateOfBirth;
    private Address address;
   .....
}

public class Address {
   private String firstLine;
   private String lastLine,
   private State state;
  ....
}


Abstract Data Types (ADT)

Abstract Data Types (ADT) are like user define types which define values and operations on that user define type without specifying how the operation will happen.

Example

In this example, we will define an ADT where will create a data type named MyString class and define operations like addition, subtraction, multiplication and division. 

User Define Data Type 

  • MyString is a wrapper object of string. 
Let's define the operations of MyString data type.

User Define Operations 

  • The addition will concatenation two MyString. Example: "Hello" + " world" will result in "Hello world".
  • The subtraction will substring the difference of length. If the difference of length is zero or negative then it will have an empty string. Example: 
    • "Hello" - "world" will result in an empty string
    • "Hello" - "hi" will result in "Hel
    • "hi" - "Hello" will also result in an empty string. 
  • The division will remove common character(s) from MyString. Example: "Hello" / "world" will result in "He".
  • The multiplication will concatenate characters alternatively. Example: "Hello" * "world" will result in "Hweolrllod".

Actual Implementation 

Sample Unit Test of the Implementation