Quantcast
Channel: Imaginate Creative Limited
Viewing all articles
Browse latest Browse all 20

Object Oriented Programming principles

0
0

Object orientated programming or OOP is the latest methodology used by software engineers to manage complex software. OOP attempts to achieve this by using four techniques known as; Abstraction, Encapsulation, Inheritance and Polymorphism. Not all programming languages implement the principles in OPP in the same way, consequently this report will also review Java’s implementation of the principles discussed.

Software development involves breaking large problems into smaller ones, managed by specific bits of code, known as functions. Tried and tested functions are commonly reused and available in libraries. OOP combines functions with related data into a single unit called a class. Before discussing OOP some considerations should be noted, such as large file size and slower run speeds. Moreover, the approach requires extensive planning before any code is written, which increases development time. However, in the long term, a considered design with less flaws can prove more beneficial. (Budd, 2002)

The first principle in OOP is abstraction, a core concept that underpins this approach in several ways. Firstly, the idea of objects in OPP are abstractions of real objects, such as a clock. Hierarchies of subclasses are further abstractions of their own base class. Importantly the process of abstraction reduces complexity to essential characteristics. This allows a programmer to ignore unrequired properties.

Screen Shot 2016-05-03 at 15.10.22

Encapsulation describes the concept of combining methods and properties to form a new data type, a class. Encapsulation ensures only it’s own methods are used to manipulate its properties. These methods form the interface which controls access to it’s properties, and prevents programmers from unnecessarily changing underlying code. Data hiding also allows parts of a system to be easily upgraded, as long the interface remains consistent. Another advantage is that knowledge of object implementation is not necessary for its use, so programmers can easily access the capabilities offered in class libraries. (w3resource, 2015)

 

When a class is needed, an instance of the class is declared, known as an object (Langfield and Bond, 2009 p.72.). Objects are usable versions of the blueprint described in a class.

Screen Shot 2016-05-03 at 15.10.51 Screen Shot 2016-05-03 at 15.10.45Screen Shot 2016-05-03 at 15.10.36

Objects have three characteristics; State, Behavior and Identity. State is defined by a classes properties, which includes primitives, e.g. int, float, double, char, or Boolean. Behaviours are implemented by functions known as methods. And identity is created with each instance (Docs.oracle.com, 2015 -a). When objects invoke methods on other objects it’s known as message passing.

Screen Shot 2016-05-03 at 15.11.09 Screen Shot 2016-05-03 at 15.11.00

One of the biggest benefits of OOP is that new classes can be based on an existing class. The new class (subclass), inherits all the methods and properties from the parent class (superclass). Additional properties and methods can be declared specific to the new class (Nixon, 2014 p.108). This technique allows programmers to repurpose classes, speeding up development time.

Through inheritance, descendant subclasses have access to all ancestor classes code and data. As illustrated, aircraft and cars are descendants of a vehicle base class. (Husband, Nguyen and Wong, 2015 p.9)

Screen Shot 2016-05-03 at 15.11.23
The final principle, polymorphism encompasses several capabilities, broadly described by, an operation being able to exhibit different behavior in different instances. One example is the ability to share common actions up and down a class hierarchy, each class can respond to the same message by a different method using. In this example a car and a jet plane respond to the message, turn left using a different method.

Screen Shot 2016-05-03 at 15.11.33

Not all programming languages implement OPP in the same way. Hybrid types such as C++ or REALbasic (Wang, 2007) use a combination of procedural and object based code, this offers manageable implementations of OOP. However, this mix causes complicated inconsistences undermining the value of implementing OPP.

True types, such as SmallTalk, C# or Java, force programmers to use objects throughout, changing a programs structure and how programmers approach development. Commonly procedural programmers think in terms dictated by the the computer (e.g. memory bytes, array’s and loops), whereas OOP allows them to think in terms of the problem being solved. Legacy code is often dealt with daily and extended to successively build on functionality while maintaining flexibility. Ultimately OOP programs remain more manageable at large scale. But can suffer from bloat when numerous objects are called for simple tasks. Performance problems cannot always be eliminated at runtime. (Xu, 2011)

In Java, a class is a definition of a group of similar objects, and acts as a template that defines states and behaviors. An instance of a class is referred to as an object, but the term object is often used loosely, referring to classes or instances.

A class can be visualized as a three-compartment box:

  1. Name (or identity): identifies the class.
  2. Variables (or state, field): contains the static attributes of the class.
  3. Methods (or behaviors, function): contains the dynamic behaviors of the class

Screen Shot 2016-05-03 at 15.11.39

In Java, the keyword class is used to define a class, as shown below. Variables are normally hidden from other classes, with private access control modifier. Access to these variables are specified via public assessor methods, such as getRadius() or getArea(). This demonstrates the principle of information hiding in encapsulation.

Screen Shot 2016-05-03 at 15.11.45

There are various ways to create an instance of a class, but all include an instance identifier, that relates to a particular class; in this case Circle, and the keyword new to allocate space for the new object. This keyword forms part of a constructor statement that establishes the instance and identifies it.

The class circle is widely used, and extends or inherits, the properties and methods of the class named shape, see figure 1. (Docs.oracle.com, 2015 -b). It is important to note that Java does not support multiple inheritance which refers to the concept of one class inheriting from more than one base class. Multiple inheritance can lead to problems in the hierarchy, this results in complexity, so most languages do not support it, with the exception C++.

Methods and variables are accessed via the dot operator, forming its interface. In the example below, circle C1 ‘s, getArea and getRadius methods have been invoked. And C2’s variables radius and color have been modified.

Screen Shot 2016-05-03 at 15.11.53

One kind of polymorphism in Java is implemented through method overloading (Academia.edu, 2015), in this example the method average has three versions with differing arguments. Polymorphism is implemented using a technique called late method binding which means the exact method to be called is determined at runtime, however late binding is slower than normal binding.

Screen Shot 2016-05-03 at 15.12.03

An object oriented program is much easier to modify and maintain than a non-object oriented program. While more time is spent implementing OPP less work is needed to maintain it over time. Organizing programs using abstractions of real objects allows programmers to develop more intuitively, building upon class libraries allows useful code to be enhanced and extended. Which ultimately allows programmers to design systems which are more closely related to real word problems. However, adopting OPP techniques alone won’t make writing software easier, but it will help to overcome some of the challenges facing software development.

 

References

Academia.edu, (2015). OBJECT ORIENTED PROGRAMMING PRINCIPLES. [online] Available at: http://www.academia.edu/6409654/OBJECT_ORIENTED_PROGRAMMING_PRINCIPLES [Accessed 20 Dec. 2015].

Budd, T. (2002). An introduction to object-oriented programming. Boston: Addison-Wesley.

Docs.oracle.com, (2015 -a). What Is an Object? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts). [online] Available at: https://docs.oracle.com/javase/tutorial/java/concepts/object.html [Accessed 19 Dec. 2015].

Docs.oracle.com, (2015 -b). Circle (JavaFX 2.2). [online] Available at: https://docs.oracle.com/javafx/2/api/javafx/scene/shape/Circle.html [Accessed 20 Dec. 2015].

Husband, M., Nguyen, D. and Wong, S. (2015). Principles of Object-Oriented Programming. [online] Available at: http://ufdcimages.uflib.ufl.edu/AA/00/01/16/50/00001/OOProgramming.pdf [Accessed 20 Dec. 2015].

Langfield, S. and Bond, K. (2009). AQA A2 computing. Cheltenham: Nelson Thornes,

Nixon, R. (2014). Learning PHP, MySQL, and JavaScript. 4th ed. Sebastopol, CA: O’Reilly.

w3resource, (2015). Java Object Oriented Programming concepts – w3resource. [online] Available at: http://www.w3resource.com/java-tutorial/java-object-oriented-programming.php [Accessed 20 Dec. 2015].

Wang, W. (2007). Beginning programming for dummies. Indianapolis, IN: Wiley Pub.

Xu, G. (2011). Analyzing Large-Scale Object-Oriented Software to Find an d Remove Runtime Bloat. Ohio, p.2.

 

Appendices

Figure 1. Java inheritance tree

based on documentation available at:

Screen Shot 2016-05-03 at 15.12.16

https://docs.oracle.com/javafx/2/api/javafx/scene/shape/Circle.html


Viewing all articles
Browse latest Browse all 20

Latest Images

Trending Articles





Latest Images