第7章高水準プログラミング言語
Encapsulationカプセル化
OOP languages contain features to implement encapsulation; they are self-contained and enable data hiding. These features include scope rules, for example, defining variables and methods within classes and using access modifiers.
英語のヒント
OOP言語には、カプセル化を実現する機能がある。これらの機能は、内部で完結し、データ隠蔽を可能にする。例として、クラス内で変数やメソッドを定義し、アクセス修飾子を使うといったスコープの規則がある。
Object-oriented languages often have constructors, and some have destructors which support encapsulation. A constructor is a specialized method that is called upon to instantiate the object. Very often, constructors have parameters which can be used to initialize the values of the attributes with arguments that are passed to them so that the variables are not directly accessed or assigned to. A destructor is used to destroy the instantiated object and recover its memory as in C++. Java and modern HLLs have a background running program for garbage collection that automatically destroys objects and recovers memory when there are no longer any references to them in the code.
英語のヒント
オブジェクト指向言語には、コンストラクターがあることが多く、一部にはカプセル化を支えるデストラクターもある。コンストラクターは、オブジェクトをインスタンス化するために呼び出す特別なメソッドである。コンストラクターには引数があることが多く、渡された実引数を使って属性の値を初期化できるため、変数へ直接アクセスしたり代入したりせずに済む。デストラクターは、C++でのように、インスタンス化したオブジェクトを破棄して、そのメモリを回収するために使う。Javaや現代の高水準言語には、バックグラウンドで動くガベージコレクションのプログラムがあり、コード内からの参照が一つもなくなると、オブジェクトを自動的に破棄してメモリを回収する。
They also employ the use of methods to both set and return the values in variables so that they are not directly accessed. Data hiding is further enabled in some modern HLLs such as Java. This engages a background running process for garbage collection that automatically destroys objects and recovers memory when there are no longer any references to them in the code.
英語のヒント
また、変数に直接アクセスしないように、値の設定と取得の両方にメソッドを使う。Javaなどの現代の高水準言語の一部では、データ隠蔽をさらに進めている。そのために、ガベージコレクションを行うプロセスがバックグラウンドで動き、コード内からの参照が一つもなくなったオブジェクトを自動的に破棄し、メモリを回収する。
Data hiding is one of the main objectives of OOP that falls under encapsulation. Its purpose is to address the details of implementation in a class or object that are irrelevant to users of the class or object. They need only know how to use them; therefore, the details are hidden and provide only what is necessary in its public interface.
英語のヒント
データ隠蔽は、カプセル化に含まれるOOPの主要な目的の一つである。その狙いは、クラスやオブジェクトの利用者に関係のない実装の細部を扱うことにある。利用者は使い方だけを知ればよい。そのため、細部を隠し、公開インターフェースでは必要なものだけを提供する。
Most OOP languages have the keywords public and private. They are used to mark the visibility and access to attributes and methods by areas of code that are outside the class code itself. We call them access modifiers. A member is an attribute or method that is encapsulated within a class or object. A public member is part of the public interface, and a private member is hidden. The following Java code is an abridged class definition that demonstrates the concept:
英語のヒント
OOP言語の多くには、publicとprivateというキーワードがある。これらは、クラスのコードの外にあるコードから、属性やメソッドを参照できるか、アクセスできるかを示すために使う。これらをアクセス修飾子と呼ぶ。メンバーとは、クラスやオブジェクト内にカプセル化された属性やメソッドである。publicメンバーは公開インターフェースの一部であり、privateメンバーは隠される。次のJavaコードは、この考え方を示すために省略したクラス定義である。
public class Rectangle {
// attributes
private double length;
private double width;
// constructors
public Rectangle(double length, double width) {
. . . . .
}
// methods
public double getLength() {
return length;
}
. . . . .
public double getArea() {
. . . . .
}
}The class itself is public, granting access to it and its contents. The attributes are all private, not part of the public interface. In this context, the designer has decided not to allow direct access to these attributes from outside. Instead, they are accessed by public methods, some of which are not shown. The methods that we employ, called getters and setters, can present our data to the outside world in any way we choose. The method can be described as a utility method that can provide the public useful data so that they do not have to compute it themselves.
英語のヒント
クラス自体はpublicなので、そのクラスと内容へのアクセスが認められる。属性はすべてprivateで、公開インターフェースには含まれない。この場合、設計者は、外部から属性に直接アクセスさせないことを選んでいる。代わりに、ここに載っていないものも含め、publicメソッドを通じてアクセスする。使用するメソッドは、ゲッターやセッターと呼ばれ、選んだ任意の方法でデータを外部へ示せる。このメソッドは、外部の利用者が自分で計算しなくても済むように有用なデータを提供する、ユーティリティメソッドと説明できる。
Inheritance継承
Figure 7.21 illustrates the implementation of single inheritance, the methodology used by most modern OOP HLLs. Classes and objects can inherit from only one parent.
英語のヒント
図7.21は、現代のOOP高水準言語の多くが使う方式である、単一継承の実装を示している。クラスやオブジェクトは、一つの親からしか継承できない。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 7.21 This UML diagram shows single inheritance in Java. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.21 このUML図は、Javaでの単一継承を示している。(出典:著作権 Rice University、OpenStax、CC BY 4.0ライセンス)
We refer to the parent class as the superclass in an inheritance relationship and the child classes as a subclass which contains the attributes and methods of its parent class. In this illustration, all the Vehicle attributes are inherited by the Car and the other subclasses with the same exact access modifiers. The same is true of all the methods.
英語のヒント
継承関係において、親クラスをスーパークラスと呼び、子クラスをサブクラスと呼ぶ。サブクラスには、親クラスの属性とメソッドが含まれる。この図では、Vehicleのすべての属性が、まったく同じアクセス修飾子のまま、Carやほかのサブクラスへ継承される。すべてのメソッドについても同様である。
Only C++ implements multiple inheritance, inheriting attributes and methods of more than one parent. This concept is exceedingly messy and can cause major problems in implementation. An example is where both parents have a method of the same name and it cannot be specified which one is inherited or overridden. To implement behavior that is shared from multiple parent classes, Java introduced the interface, which can be implemented in C++ to solve multiple inheritance issues.
英語のヒント
複数の親から属性やメソッドを継承する多重継承を実装しているのは、C++だけである。この考え方は非常に入り組んでおり、実装に大きな問題を引き起こすことがある。たとえば、両方の親が同じ名前のメソッドを持ち、どちらが継承され、またはオーバーライドされるのかを指定できない場合がある。複数の親クラスが共有する振る舞いを実装するため、Javaはインターフェースを導入した。これは、多重継承の問題を解決するためにC++でも実装できる。
An interface resembles a class definition, but it is a collection of methods only. These methods are constructed as abstract methods. An abstract method is declared without a code block for implementation. A class can use one or more interfaces. The following is a Java class definition that makes use of an interface:
英語のヒント
インターフェースはクラス定義に似ているが、メソッドだけを集めたものである。これらのメソッドは抽象メソッドとして作る。抽象メソッドは、実装のためのコードブロックを伴わずに宣言される。クラスは、一つ以上のインターフェースを利用できる。以下は、インターフェースを利用するJavaのクラス定義である。
public class Rectangle implements geometryMath{
}The interface acts as a contract with the programmer. If the programmer declares that a class implements an interface, it is required that the class provide implementation code for the functions that make up all of the interface. An override defines another method with the same name and modifies its signature. The override forms a concrete instance of what was an abstract method in the interface. Interfaces appear in Java, C#, Go, or Ruby, and are the dominant approach, superseding true multiple inheritance.
英語のヒント
インターフェースは、プログラマーとの契約の役割を果たす。あるクラスがインターフェースを実装すると宣言したなら、そのインターフェースを構成するすべての関数について、クラスが実装コードを提供しなければならない。オーバーライドでは、同じ名前を持つ別のメソッドを定義し、そのシグネチャーを変更する。これによって、インターフェースでは抽象メソッドだったものが、具体的な実装となる。インターフェースはJava、C#、Go、Rubyに見られ、真の多重継承に代わる主流の方法となっている。