Java encapsulation and interfaces with examples

22-03-2023

encapsulation

Encapsulation is one of the four fundamental concepts of OOP. The other three are inheritance, polymorphism, and abstraction.

Encapsulation is the technique of making the fields of a class private and providing access to the fields through public methods. If a field is declared private, no one outside the class can access it, which hides the fields inside the class. For this reason, encapsulation is also called data hiding.

Encapsulation can be described as a protective barrier that prevents code and data from being randomly accessed by other code defined outside of the class. Access to data and code is strictly controlled by an interface.

The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others using our code. With this feature, encapsulation brings maintainability, flexibility, and extensibility to our code.

Example:

Let’s see an example that represents encapsulation:

/* File name: EncapTest.java */

public class EncapTest{

private string name;

private string idNum;

private internal age;

public int getAge(){

return age;

}

publicString getName(){

return name;

}

public string getIdNum(){

return idNum;

}

public void setAge(int newAge){

age = newAge;

}

public void setName(String newName){

name = newName;

}

public void setIdNum(String newId){

idNum = newId;

}

}

Benefits of encapsulation:

• The fields of a class can be read-only or write-only.

• A class can have full control over what is stored in its fields.

• Users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class don’t need to change any of their code.

interfaces

An interface is a collection of abstract methods. A class implements an interface, so it inherits the abstract methods of the interface.

An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.

Unless the class that implements the interface is abstract, all methods of the interface must be defined in the class.

An interface is similar to a class in the following ways:

• An interface can contain any number of methods.

• An interface is written to a file with a .java extension, with the interface name matching the file name.

• The bytecode of an interface appears in a .class file.

• Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

However, an interface is different from a class in several ways, including:

• You cannot instantiate an interface.

• An interface does not contain constructors.

• All the methods of an interface are abstract.

• An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.

• An interface is not extended by a class; is implemented by a class.

• An interface can extend multiple interfaces.

Interfaces declaration:

The interface keyword is used to declare an interface. Here is a simple example to declare an interface:

Example:

Let’s see an example that represents encapsulation:

/* File name: InterfaceName.java */

import java.lang.*;

//Any number of import statements

public interface NameOfInterface

{

//Any number of final static fields

//Any number of abstract method declarations

}

Interfaces have the following properties:

• An interface is implicitly abstract. You don’t need to use the abstract keyword when declaring an interface.

• Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.

• Methods on an interface are implicitly public.

Leave a Reply

Your email address will not be published. Required fields are marked *