Abstraction is one of the 4 pillars of object-oriented programming. The main purpose of abstraction is to hide implementation details and show only the required/essential information. In simple words, it reduces complexity for normal users. It is mainly used to solve the issues at the design level, mostly the conceptual part.
Abstraction can be achieved via Abstract classes and Interfaces. Through abstract classes, java can’t achieve true abstraction, but by the use of interfaces, it can achieve 100% abstraction. Let’s see how.
In this Article, I will discuss Interfaces.
Multiple inheritance is not allowed in java, meaning one class cannot extend multiple classes because if such a case happened then the child class cannot differ which method came from which parent class. In such a scenario we can use Interfaces to enjoy the benefits of multiple inheritance and here is how.
Interfaces: An Interface is a set of specifications that a class must implement. By default, interfaces are public and abstract, so there is no need of defining them explicitly. Let us take an example.
In the below code, an interface named Engineer is created which means any class that implements Engineer must have to use its defined methods. If you see inside interface PracticeEngineering, it is neither defined public nor abstract implying that by default methods inside interfaces are public and abstract, there is no harm if you define also.
package Interfaces;
public interface Engineer {
void PracticeEngineering();//by default abstract and public
}
We have one more interface called Doctor.
package Interfaces;
public interface Doctor{
void performSurgery();
void beomceDoctor(); //by default abstract and public
}
Suppose a person class named “Student” want to become an engineer as well as a doctor. Now, there is no hierarchy between Engineer and Doctor, both the professions differ from each other, meaning neither engineer can become sub-class of doctor nor doctor can become sub-class of engineer; both of them are at the same level. Such cases come under multiple inheritance that is solved using interfaces.
A class can implement multiple interfaces in order to acquire both of their benefits.
package Interfaces;
public class Student implements Engineer, Doctor{
public static void main(String[] args) {
Person obj = new Person();
obj.PracticeEngineering();
obj.performSurgery();
obj.beomceDoctor();
}
@Override
public void performSurgery() {
System.out.println("Person is performing surgery");
}
@Override
public void PracticeEngineering() {
System.out.println("Person is pursuing Engineering");
}
@Override
public void beomceDoctor() {
System.out.println("Person is pursuing MBBS");
}
}
NOTE
- A child class can extend only one parent class in java ( multiple inheritance is not allowed in java)
- One interface can extend multiple interfaces Ex: Interface “VideoEditing” can extends two Interfaces EditVideo and UploadVideo.
interface EditVideo {
void videoEditor();
}
interface UploadVideo {
void videoUploader()
}
public interface VideoEditing extends EditVideo, UploadVideo {
void StudyConcept();
}
- When a class implements an interface it must include implementation for all its methods.
Ex: If a class “Blogger” implements a VideoEditing interface then it should include methods videoEditor() and videoUploader().
public class Blogger implements VideoEditing {
@Override
void StudyConcept() {
System.out.println("Blogger is Studing concept");
}
@Override
void videoEditor() {
System.out.println("Blogger is editing video");
}
@Override
void videoUploader() {
System.out.println("Blogger is uploading video");
}
}
Thanks for spending your valuable time reading this article, I am sure you learned something today.😄
Top comments (0)