Perfect Developer basic tutorial 1 | This page last modified 2011-10-28 (JAC) |
There are many opinions of what it means for a development system or language to be "object-oriented". However, it is widely agreed that any language that claims to be object-oriented must support encapsulation, inheritance, polymorphism and dynamic binding.
Encapsulation means that the detailed implementation of a component is hidden from the rest of the system. Specifically, the data held by a component is normally not accessible from the outside of the component. Instead, a number of interface functions and procedures (collectively called methods) are provided for use by those parts of the software system that are clients (i.e. users) of the component.
The way most object-oriented language support encapsulation is to allow the user to declare classes. A class is a collection of data variables (sometimes called attributes), private methods and public (or interface) methods, together with one or more special methods called constructors. A constructor for a class creates a new instance of the class (i.e. a new object with its own copies of the data variables). Instances can be stored in variables, passed as parameters, etc. but the data within an instance cannot be directly accessed. Instead, the interface methods of the class can be invoked on the particular instance to modify the values of the data variables and/or return information about them.
The usual way to express a method call on an instance is to use the "dot" notation.
For example, if class BankAccount declares an interface method withdraw which takes a parameter of type Money, and if myAccount is a variable of type BankAccount that has been initialized to some instance of BankAccount, then the syntax myAccount.withdraw(someMoney) means call withdraw on the instance myAccount with the parameter someMoney. Effectively, the instance myAccount is passed as an additional parameter to withdraw. Within the definition of withdraw, the value of this "current instance" parameter is usually referred to using a reserved word, such as this or self.
Inheritance is a facility to declare new classes by extending old ones.
For example, given the declaration of class BankAccount, you could declare a new class SavingsAccount to extend (or inherit from) BankAccount. Then SavingsAccount would inherit all the data and methods of BankAccount. In declaring SavingsAccount, you are free to add new variables (e.g. InterestRate) and new methods, or redefine inherited methods (i.e. override the inherited definition with a new one).
Polymorphism ("many structures") is a facility by which one type can be substituted for another.
For example, in Perfect, if you declare a variable of type "from BankAccount", you are free to store a variable of type SavingsAccount (or any other class derived from BankAccount) there, or just a plain BankAccount. This works because SavingsAccount supports all the methods that BankAccount does.
Dynamic binding means that where you are using polymorphism and you call a method, the "version" of the method that actually gets called depends on the actual type(s) of the instance(s) involved.
For example, suppose you declared BankAccount and then SavingsAccount, but in declaring SavingsAccount you redefined the inherited method withdraw. This gives you two versions of withdraw. Then, each time a call is made to myAccount.withdraw(someMoney), the version of withdraw that is called will depend on whether myAccount holds a plain BankAccount, or a SavingsAccount.
Save My Place | Glossary | Language Reference Manual |
Copyright © 1997-2012 Escher Technologies Limited. All rights reserved. Information is subject to change without notice. |