---
title: "Is the purpose of an interface to hide information?"  
description: "Is the purpose of an interface to hide information?"  
author: "Anonymous User"  
published: 2013-06-12  
updated: 2023-07-04  
canonical: https://www.mindstick.com/forum/1054/is-the-purpose-of-an-interface-to-hide-information  
category: "oops"  
tags: ["oops"]  
reading_time: 3 minutes  

---

# Is the purpose of an interface to hide information?

Given that [interfaces](https://www.mindstick.com/articles/12100/interfaces-in-java-real-life-example) are also used to help hide information, [giving](https://yourviews.mindstick.com/view/80639/regifting-is-far-better-than-giving-away-a-bouquet) the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) only a subset of the possible [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) they are allowed to use, and, let's [say](https://answers.mindstick.com/qa/116645/a1-wreckers-reviews-what-do-customers-say), I have a Person [class and interface](https://www.mindstick.com/forum/23098/difference-between-abstract-class-and-interface-in-java) IPerson.

[Now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) in main, I'm allowed to do

```
IPerson testy = new Person();
```

or

```
Person testy = new Person();
```

So really I'm not restricted from using Person still. So how does the

interface truly hide [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science)?

## Replies

### Reply by Aryan Kumar

No, the purpose of an [interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces) is not to hide information. The purpose of an interface is to **specify a contract** between different classes. This contract defines the methods that a class must implement in order to be considered an implementation of the interface.

An interface can be used to hide information in some cases. For example, if an interface defines a method with no implementation, then the implementation of that method can be hidden from classes that implement the interface. However, this is not the primary purpose of an interface.

The primary purpose of an interface is to **promote polymorphism**. Polymorphism is the ability to use a variable of one type to refer to an object of another type. This can be useful for making code more flexible and reusable.

Here is an example of how an interface can be used to promote polymorphism:

Java

```plaintext
interface IPrintable {
    void Print();
}

class Book implements IPrintable {
    public void Print() {
        System.out.println("This is a book");
    }
}

class Article implements IPrintable {
    public void Print() {
        System.out.println("This is an article");
    }
}

public class Main {

    public static void main(String[] args) {
        IPrintable printable = new Book();
        printable.Print();

        printable = new Article();
        printable.Print();
    }
}
```

This code will first create an object of the `Book` class and assign it to a variable of type `IPrintable`. Then, it will call the `Print()` method on the variable. The `Print()` method will be called on the object of the `Book` class, and the text "This is a book" will be printed to the console.

Next, the code will create an object of the `Article` class and assign it to the variable of type `IPrintable`. Then, it will call the `Print()` method on the variable. The `Print()` method will be called on the object of the `Article` class, and the text "This is an article" will be printed to the console.

As you can see, the `IPrintable` interface allows us to use a variable of type `IPrintable` to refer to objects of different types. This is polymorphism in action.

### Reply by Vijay Shukla

Hey Jacob Rasel!

What if You dont have that Person [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp).

\

```
 Object obj = loadObject();  if(obj is IPerson) {   IPerson person = (IPerson) obj;  }
```

\

or

```
  IPerson person = loadPerson();
```


---

Original Source: https://www.mindstick.com/forum/1054/is-the-purpose-of-an-interface-to-hide-information

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
