Interfaces And Data Types

Interfaces? What are they?

Basically, interfaces are classes just like abstraction, the only difference is that in abstraction we can include abstract and non-abstract methods in abstract class but this is not the case in interfaces. We cannot use the non-abstract method in interfaces. So we can say that interfaces are a collection of abstract methods. We cannot extend the interface class to another class. Interfaces are implement by another class.

Also, the interface does not contain any constructor which means we can instantiate the interface class. We use the keyword interface to declare an interface class and implement keywords to implement in the interface.

Example-In the ATM when we request money, the machine undergoes some process and then returns us the requested money. So only requested money is available to us and the whole implementation part of how atm processes the requested money is not visible to us.

CODE-:

Moreover, if we use the keyword extends it will throw an error.

Why Interfaces?

By using interfaces we can achieve multiple inheritances in java. Moreover, interfaces provide total abstraction and we can achieve loose coupling(situation when our object gets the object that is to use from outside). Moreover, all the methods in interfaces are public and abstract because the developer can change or add new methods and it won’t affect the classes that implement interfaces.

What Interfaces contains?

  • Constant values with public access specifier only.
  • Default, static, and private methods with implementations. 
  • Abstract method without its implementations.

Multiple Interfaces

As we know that multiple inheritance is not support in Java, developers introduce a concept as multiple interfaces in which an interface can implement more than one parent class. In short with the help of interfaces we can apply multiple inheritances in java.

Are all the functions mention inside the interface overridden?

All the methods inside the interface are override.

Data Types in Java

Datatypes specify the size and type of data we are using or want to store.

For example, if we want to store an integer value then we have to use the keyword int and the name of the variable in which we want to store.

Now let us understand these terms:-

1. Primitive Datatypes

Primitive Datatypes cannot be further broken down into smaller parts. For example, we cannot further break down an integer value.

Primitive Datatypes=int ,boolean , long, short, byte,double ,float ,char.etc

2. Non -Primitive Datatypes

On the other hand, Non-primitive Datatypes are those type of Datatypes which can be broken down in smaller parts. For example, arrays consist of elements so they can be broken down into smaller parts hence they are non-primitive datatype.

Non primitive Datatypes=Arrays,String ,ArrayList,HashMaps,LinkedList etc.

source=Geeksforgeeks.com

Floating Point=Floating point data types are just like integer but they can store decimal values.

Now let us write the program for each data type.

Why do we need Data Types?

We need datatypes in java because it is a strongly typed language, moreover, we need a variable to store our data in it. Therefore it is necessary that a variable has a defined data type. Data types also give an idea to the reader about what type of value or what type of data can be or is stored in the variable.

Difference Between primitive and non-primitive data types

Primitive data types are present in the smallest form possible and store particular data in them. For example- int can only store an integer number and it cannot be divided further.

Whereas,non-primitive data types are those which can be divided further into smaller parts and are not particular in storing data. For example-Arrays can store char, string, int and they can be divided further into elements.

Abstract Data type(ADT)

Abstract data types are basically data types whose behavior is defined by the values and operations we assign them.

these data type only shows what operations they will perform, they don’t show the implementation, memory allocation,  and algorithm.

source-GeeksforGeeks

For example-In the ATM when we request money, the machine undergoes some process and then returns us the requested money. So only requested money is available to us and the whole implementation part of how atm processed the requested money is not visible to us.

Operations in abstract data types

The following are the operations used in ADT

1-get()-Using this operation we can get the position of a specific element present. If no such element exists it throws an error.

2-insert()-By using the insert operation we can insert the elements and data we want to insert in the array.

3-remove()-This operation removes the element we mention from the array.

4-removeAt()-This function removes all the elements that are present in the array and provides us an empty array.

5-replace()-By using this function we can replace the old element by entering the new element at the given position.

6-size()-This function represents the size of the array.

7-isEmpty()-This function checks whether the array is empty or not and returns a boolean value(true or false).

8-isFull()-This function is similar to isempty().It returns boolean value ie. true if the array is full and false if the array is not full.

Are non-primitive data types an object data type?

Non-primitive data types refer to the object and hence they are called reference data types. For example, Array is a non-primitive data type (can store any type of value and can be divided into smaller units), which refers to the object and hence called a reference data type.

The Interface is which data type and why?

The Interface is a non-primitive or reference data type. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.

Conclusion

Java is a strongly typed language therefore data types play an important role.

With the help of data types, we can store our values or data, moreover, data type defines the operations that can be done on the data.

Multiple inheritances are not supported in java but with the help of interfaces, we can inherit multiple interfaces and also can inherit multiple parent classes. Moreover, interfaces provide total abstraction hence providing the user with only the necessary details.

written by: Aakarsh Vats

Reviewed By: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

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