Learning a New Programming Language Part 2: Language Types - Code reuse and inheritance
(Page 2 of 4 )
Object oriented languages are designed around the idea of reusing code. Careful coding can allow a class (which is a template for making objects) or hierarchical group of classes (often called a package) to be used in multiple projects by providing a generic service to the application, such as interfacing with a network protocol or parsing and processing any XML file. Every time you have a project that needs that type of functionality, you use that class or group of classes to provide it. There is no need to write that code again and again.
Java and C# are both examples of languages that were designed with object orientation from the start. In both languages, even the main application code is contained in a class.
PHP and Perl are examples of procedural languages that have had OO features added on. Both can be used in an object oriented fashion. However, in the case of PHP, for instance, there is no such thing as a private method. In OO private methods are for use internally by the class or object made from a class. Other code or objects are generally not allowed access to private methods.
Inheritance is considered another key OO feature. In an OO language classes are arranged in a hierarchy. Superclasses, which are at the top of the hierarchy, are very generic basic templates of objects. Below this are subclasses that inherit properties and methods from the superclass. A motorcycle superclass, for instance, could contain properties and methods that are common to all motorcycles. Subclasses of this class could represent different types of motorcycles.
We could drill down as far as we needed to in this example. In fact our motorcycle Superclass could actually be a subclass of a vehicle class that contains properties and methods that are common to all vehicles.
The details of inheritance, polymorphism and interfaces are beyond the scope of this article, but this should give you a start. In order to get your head around OO you will need to understand the concepts that make it unique. Moving from a procedural language to OO is not as hard as it sounds, but it is more than just syntax. Try to find material that demonstrates the concepts in a visual manner and don't let the buzzwords bother you.
Languages that are generally considered object oriented by design include C++, Objective C (an object oriented subset of C), Smalltalk and Java to name a just a few.
More Web Hosting How-Tos Articles
More By Chris Root