Programming Java Java Interfaces | Java Abstraction | Properties of OOP

Java Interfaces | Java Abstraction | Properties of OOP

-

Object define their interaction with the outside word through the method that they expose. In Java abstraction, we hide method implementation from method declaration. So, the most common form of java interfaces are group of related abstract methods without its body or implementation, and nothing else. However, we can also declare fields, or variables in an interface. 

Previously we have discussed that there are two way to achieve abstraction in java. One of which was using abstract class, and another way is using interfaces. In this article we will discuss about java interfaces.

Using interfaces is the best way to achieve abstraction in java. Because in interfaces there will be nothing other than abstract methods, and its subclasses contain the implementation for those methods. In interface it doesn’t actually require a method to be explicitly declared as abstract method. Any method which is in java interface is an abstract method.

Example of Java interfaces

Java interfaces are similar to java classes, but interfaces are not any class. Because a class describe the behavior of an object. On the other hand, interface contains the behavior that a class describes or implements.

public interface Shape {
    public double PI = 3.1416;
    void draw(int r);
    void resize();
}

As interface is a reference type therefore, interface cannot be instantiated. So other class can implement the interface. Therefore, we can access field from interface as we access field from a class.

System.out.println(Shape.PI);

But to access method, we first need to describe, or implement the method in a class.

public class Circle implements Shape {
   public double area;
   
   //describing draw method.
   public void draw (int r) {
     area=PI*r*r;
     System.out.println(area);
     }
}

Here in above code, Circle class described or implemented the draw method. Now we can access it using following way:

Shape s = new Circle();
s.draw(5);

This is the end of basic discussion on java interfaces. Next we will talk about java polymorphism here »

For java basic understanding, read java docs.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap comparing to other languages. It stores data as a key-value...

Hash Table | Indexing | Hashing Algorithm | Python Implementation

This article will talk about a high-level view of the Hash Table. As a programmer, this technique...

Eigenvector Eigenvalue | Linear Algebra Fundamentals

Eigenvector ($bar{v}$) in linear algebra is a non-zero vector (matrix) that doesn't change its direction during linear...

Pivot Table | Microsoft Excel | Create Data Insight Easily

Pivot table in microsoft Excel is an useful function that gives us a way to create insight...

Macro Function in Microsoft Excel | Automate Repetitive Task

This article we will talk about the Macro. It is a function in microsoft excel which basically...

SVD | Singular Value Decomposition | Machine Learning Fundamentals

Singular Value Decomposition or SVD is the general purpose useful tool in Numerical Linear Algebra for data...

Must read

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap...

Eigenvector Eigenvalue | Linear Algebra Fundamentals

Eigenvector ($bar{v}$) in linear algebra is a...

You might also likeRELATED
Recommended to you