Encapsulation and Interface
Encapsulation and Interface
Interfaces are a reference type, similar to a class, which can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

To use an interface, you write a class that implements the interface. When an instantiable class implements an interface, it provides a method body for each of the methods declared in the interface. For example,

public class OperateBMW760i implements OperateCar {
// the OperateCar method signatures, with implementation —
// for example:
int signalTurn(Direction direction, boolean signalOn) {
//code to turn BMWs LEFT turn indicator lights on
//code to turn BMWs LEFT turn indicator lights off
//code to turn BMWs RIGHT turn indicator lights on
//code to turn BMWs RIGHT turn indicator lights off
}
// other members, as needed — for example, helper classes
// not visible to clients of the interface
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.
Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding. Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface. The main benefit of encapsulation is the ability to modify a implemented code

Get Your Essay

Cite this page

Main Benefit Of Encapsulation And Source Code. (June 16, 2021). Retrieved from https://www.freeessays.education/main-benefit-of-encapsulation-and-source-code-essay/