Abstraction and Interface in Java - ProgrammerTech


Abstraction and Interface in Java

  • Share this:
Abstraction and Interface in Java

Abstract Classes in java

Abstraction When we define an abstract class, this means that this class is only intended for declaring or declaring functions and variables only, and we cannot build code inside the abstract class. This is its definition in short and is announced using the word abstract and then the name of the class. Even functions and variables that are inside the abstract class must be preceded by the word abstract to define it as abstract.

The general form of writing an abstract in Java

package ****;
abstract class //Here we write the name of the class
    abstract void names();  names //is the name of the function
public class ****{

If you define a class as abstract, this means that the class cannot create objects from it.

Example 1

We note in this example that we created an abstract class with the name car, and inside it was also a function preceded by the word abstract, which you know is abstract. And when we open the parentheses below it to give a print command, it will give us an error, a red line will appear below the function because of the conditions it is not possible to create a code inside it and it is only intended for declaring functions.


The purpose of using abstraction

The goal is to compulsorily implement a specific pattern. For example, we have two, three, four important functions and we want to implement them in each or more than one class, and we want to implement them compulsorily so as not to forget them. We will face the problem of forgetting. For example, we build a class and forget to implement an important function that must be implemented. To avoid forgetting, we build an abstract class and then declare these important functions. And then when creating the class we make it inherit from the abstract class in order to compulsorily force it to execute all the functions defined within the class.

Example 2

package cara;
abstract class car
    abstract void color();
    abstract void speed();
class carB extends car
    void color()
    void speed()
public class CarA{
     public static void main(String []args){
        System.out.println("Hello World");

We created a simple class named car with two void functions, color and speed. Then we inherited a class called carB from the father class and inherited the functions of color and speed and then printed them through the well-known print command System.out.println, and here we were able to announce the functions in the father class.

Abstraction is a technique used to hide program execution details


Interface in java

Interfaces are called in the Arabic language the Interface, and it is similar in structure to a class and contains abstract functions of the type abstract. And we mentioned that the word “abstract” means that the function or the class whose type is just abstract, that it does not contain code, so it is used for declaration only. The Interface is similar to the abstract class, that is, it contains abstract functions of type abstract and cannot accept building code inside them.

Interface is called in some references the term pattern, meaning that the class that will be defined from Interface is binding. Executes all functions inside the Interface. It is similar to the abstract class, except that the difference between it and the Interface is that it accepts multiple inheritance.


Difference between Interface and abstract in Java

  1. The abstract class can contain both abstract and non-abstract functions, while the Interface class contains only abstract functions, meaning they are preceded by the word abstract.
  2. The abstract class does not support multiple inheritance as is the case in other classes, while Interface supports multiple inheritance.
  3. An abstract class can contain both static and variable variables, while Interface contains only static variables.
  4. The reserved word abstract is used for classes and functions, while the word Interface is used for interfaces only.


What does the interface in java contain?

The Interface contains only abstract functions, and the inherited class must implement all the functions in the Interface. This makes us understand that the Interface controls the path of the program, so it is called in some references the term pattern or the drawn path. It restricts the program to a specific path, which is that all classes inheriting from Interface implement all Interface functions and static variables.


How to inherit class from Interface Java

In the previous lessons, we learned that when a class wants to inherit from another class, the word extends is used, but. When a class wants to inherit from Interface we use the implements keyword.

General form of class inheritance from Interface Java

class A implements P

It means we have a class named A and the Interface is inherited by the word implements.

Example 3

package c1;
interface A1
    void print();
public class C1 implements A1{
    public void print()
        System.out.println("welcome course java")
     public static void main(String []args){
        C1 c=new C1();

We created an Interface with the name A1 with a function named print and we inherited a class named C1 from the Interface named A1. And we called the function inside it and put a code to print the sentence welcome course java. We entered the main program and created an object from class C1 and called the print function from object c.


The difference between extends and implements in Java

  • Extends means inherit and we use it to make a class inherit from a class or to make Interface inherit from one or more interfaces.
  • implements means the implementation we use to make a class implement one or more interfaces.

Interface variables are defined as public final static even if we don't define them as well.

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com