Atm Sample Program In Java
In the last tutorial we discussed abstract class which is used for achieving partial abstraction. Unlike abstract class an interface is used for full abstraction. Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user(See: Abstraction). In this guide, we will cover what is an interface in java, why we use it and what are rules that we must follow while using interfaces in Java Programming.
What is an interface in Java?
Jan 12, 2012 I still remember my teacher in Object-Oriented Programming using Java gave us a Lab Examination (hands-on exam) about how to use classes and methods. The problem he gave us is to create a simple ATM machine program wherein it allows the user to select transactions such as to withdraw, deposit and check or inquiry balance. C programming is perfect for beginners and hence our choice to create an ATM Machine program. I had initially created an ATM machine using java programming language, applying the same concept used here. It’s essential for you to have some simple basics of C programming before proceeding. Chrysler diagnostic software free download.
Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body, see: Java abstract method). Also, the variables declared in an interface are public, static & final by default. We will cover this in detail, later in this guide.
What is the use of interface in Java?
As mentioned above they are used for full abstraction. Since methods in interfaces do not have body, they have to be implemented by the class before you can access them. The class that implements interface must implement all the methods of that interface. Also, java programming language does not allow you to extend more than one class, However you can implement more than one interfaces in your class.
Syntax:
Interfaces are declared by specifying a keyword “interface”. E.g.:
Example of an Interface in Java
This is how a class implements an interface. It has to provide the body of all the methods that are declared in interface or in other words you can say that class has to implement all the methods of interface.
Do you know? class implements
interface but an interface extends
another interface.
Output:
You may also like to read:Difference between abstract class and interface
Interface and Inheritance
As discussed above, an interface can not implement another interface. It has to extend the other interface. See the below example where we have two interfaces Inf1 and Inf2. Inf2 extends Inf1 so If class implements the Inf2 it has to provide implementation of all the methods of interfaces Inf2 as well as Inf1.
Learn more about inheritance here: Java Inheritance
In this program, the class Demo
only implements interface Inf2
, however it has to provide the implementation of all the methods of interface Inf1
as well, because interface Inf2 extends Inf1.
Tag or Marker interface in Java
An empty interface is known as tag or marker interface. For example Serializable, EventListener, Remote(java.rmi.Remote) are tag interfaces. These interfaces do not have any field and methods in it. Read more about it here.
Nested interfaces
An interface which is declared inside another interface or class is called nested interface. They are also known as inner interface. For example Entry interface in collections framework is declared inside Map interface, that’s why we don’ use it directly, rather we use it like this: Map.Entry
.
Key points: Here are the key points to remember about interfaces:
1) We can’t instantiate an interface in java. That means we cannot create the object of an interface
2) Interface provides full abstraction as none of its methods have body. On the other hand abstract class provides partial abstraction as it can have abstract and concrete(methods with body) methods both.
3) implements
keyword is used by classes to implement an interface.
4) While providing implementation in class of any method of an interface, it needs to be mentioned as public.
5) Class that implements any interface must implement all the methods of that interface, else the class should be declared abstract.
6) Interface cannot be declared as private, protected or transient.
7) All the interface methods are by default abstract and public.
8) Variables declared in interface are public, static and final by default.
All of the above statements are identical.
9) Interface variables must be initialized at the time of declaration otherwise compiler will throw an error.
Above code will throw a compile time error as the value of the variable x is not initialized at the time of declaration. Bi publisher template builder for word 2016.
10) Inside any implementation class, you cannot change the variables declared in interface because by default, they are public, static and final. Here we are implementing the interface “Try” which has a variable x. When we tried to set the value for variable x we got compilation error as the variable x is public static final by default and final variables can not be re-initialized.
11) An interface can extend any interface but cannot implement it. Class implements interface and interface extends interface.
12) A class can implement any number of interfaces.
13) If there are two or more same methods in two interfaces and a class implements both interfaces, implementation of the method once is enough.
14) A class cannot implement two interfaces that have methods with same name but different return type.
15) Variable names conflicts can be resolved by interface name.
Advantages of interface in java:
Advantages of using interfaces are as follows:
- Without bothering about the implementation part, we can achieve the security of implementation
- In java, multiple inheritance is not allowed, however you can use interface to make use of it as you can implement more than one interface.