Object Oriented Programming Ray John Pamillo 1/27/2016 1 Nokia Solutions and Networks 2014
Outline: Brief History of OOP Why use OOP? OOP vs Procedural Programming What is OOP? Objects and Classes 4 Pillars of OOP Interfaces Demo / Exercises 2 Nokia Solutions and Networks 2014
Brief History of OOP (Object Oriented Programming) - 1950 s to 60 s : object as items (LISP atoms) with properties - 1961 : Simula 67, introduced the concepts of OOP - 1966 : Alan Kay, while working with Ivan Sutherland (doing graphics programs), developed his views on objects and programming - 1968 : Kay met Seymour Papert and learned of Logo, a Lisp dialect - 1970 s : Smalltalk, by Alan Kay at Xerox Parc, is considered the pure OO language (everything are objects). This work influenced the Lisp community to adopt OOP features. OOP gained momentum at this time. - 1980 s : C++, by Bjorn Stroustrup, is an integration of OOP into C language. - 1990 s : Java, by James Gosling at Sun Microsystems, a simpler version of C++ 3 Nokia Solutions and Networks 2014
4 Nokia Solutions and Networks 2014 Why Use OOP?
Why Use OOP? - It is widely used. - It improves maintainability of large software projects. - It encourages software reuse. - It makes programming easier. - It can lead to more expressive code. function dothemostexpressivethingpossible(){ // do something expressive here 5 Nokia Solutions and Networks 2014
6 Nokia Solutions and Networks 2014 OOP vs PP
Object Oriented Programming Procedural Programming - Gives importance on objects and data. - Data is bundled. - Realistic Modeling. - Scalable and reusable - Easier to maintain. - Resilient to change. - Can make simple problems complex - Program flow can be difficult to track - Tends to consume more memory - Gives importance on operations. - Data is exposed. - Difficult to relate with real world objects. - Difficult to create new data types. - Debugging/refactoring becomes harder as the codebase grow - Relatively simple. - Program flow is straightforward. - Needs less memory. 7 Nokia Solutions and Networks 2014
8 Nokia Solutions and Networks 2014 What is OOP?
Object Oriented Programming - It is a design philosophy. - It is about objects objects in the program correspond to real world objects, hence the term modeling 9 Nokia Solutions and Networks 2014
Real-world Objects - They all have STATE and BEHAVIOR. Software Objects - Consists of state and behavior. - Stores its state in FIELDS (or variables) and exposes its behavior through METHODS (or functions). 10 Nokia Solutions and Networks 2014
Benefits of bundling code into objects Modularity Information-hiding Code re-use Pluggability and debugging ease 11 Nokia Solutions and Networks 2014
Class - A representation of a type of object. - A blueprint, plan or template that describes the details of an object. - A factory, that is able to mass produce objects. Object - An instance of a class. 12 Nokia Solutions and Networks 2014
Class class Person { Object Person objectperson = new Person(); 13 Nokia Solutions and Networks 2014
The Class as a Blueprint is composed of A declaration of a set of variables that the object will possess. A declaration of the set of operations that the object will provide. A set of function definitions that implements each of these operations. 14 Nokia Solutions and Networks 2014
Components of a Class Instance Variables the set of variables that the object will possess. hold the state of a particular object Methods the set of operations that the object will provide. functions 15 Nokia Solutions and Networks 2014
A Class example in Java public class Person{ private String name; private int age; public Person(){ public String getname(){ return name; public int getage(){ return age; 16 Nokia Solutions and Networks 2014
Things to remember: DRY Don t Repeat Yourself KISS Keep It Simple, Stupid 17 Nokia Solutions and Networks 2014
18 Nokia Solutions and Networks 2014 The 4 Pillars of OOP
The 4 Pillars of OOP Abstraction Encapsulation Inheritance Polymorphism 19 Nokia Solutions and Networks 2014
Abstraction Representation of the essential characteristics of an object/class that differentiates it from other objects/classes with respect to the viewer s perspective. The concept of moving the focus from the details and concrete implementation of things, to the types of things (i.e. classes), the operations available (i.e. methods), etc, thus making the programming simpler, more general, and more abstract. 20 Nokia Solutions and Networks 2014
Abstraction The abstract keyword. Use of methods without body / implementation. 21 Nokia Solutions and Networks 2014
Encapsulation Hiding of unnecessary behaviors/attributes from the user and show only those that the users can directly use. Hiding of Implementation 22 Nokia Solutions and Networks 2014
Encapsulation Use of the access keywords: public, private, protected Use of getters and setters - Keep the variables private, and expose only the relevant methods when modifying these variables 23 Nokia Solutions and Networks 2014
Inheritance A mechanism which helps you to generalize common attributes and behaviors in separate classes from various subclasses. The relationship between the Parent Class and the Children Classes has an Is A relationship 24 Nokia Solutions and Networks 2014
Inheritance Use of the extends keyword Method overriding 25 Nokia Solutions and Networks 2014
Polymorphism Ability of an object to behave differently under different circumstances. An alternative to if else if else and switch case statements. 26 Nokia Solutions and Networks 2014
Interfaces a group of related methods with empty bodies The interface as an abstraction. The interface as an encapsulation. The interface as a means for polymorphism. Programming to an interface. - Interfaces are just CONTRACTS or signatures and they don t know anything about implementations 27 Nokia Solutions and Networks 2014
Interfaces : Examples public interface Vehicle { void start(); void turn(string direction); public interface Flight { int getaltitude(); 28 Nokia Solutions and Networks 2014
Programming to an Interface public class Car implements Vehicle { void start(){ // car implementation of start void turn(string direction){ // car implementation of turn public class Airplane implements Movement, Flight { void start(){ // airplane implementation of start void turn(string direction){ // airplane implementation of turn //Flight specific methods int getaltitude(){ int altitude=o; // altitude implementation return altitude; 29 Nokia Solutions and Networks 2014
Programming to an Interface public class TestVehicles { public static void main(string [] args){ Vehicle vehicle1 = new Car(); Vehicle vehicle2= new Plane(); Vehicle[] vehicles = {vehicle1, vehicle2; for(i=0; i<vehicles.length; i++){ vehicles[i].start(); vehicles[i].turn( right ); vehicles[i].turn( left ); 30 Nokia Solutions and Networks 2014
31 Nokia Solutions and Networks 2014 DEMO
References: McLaughlin, B., Pollice G., & West D. (2007). Head First : Object-Oriented Analysis and Design. USA: O Reilly Media Inc. The Java Tutorials. Retrieved Jan 25, 2016 from https://docs.oracle.com/javase/tutorial/java/concepts/ Difference Between Object-oriented Programming and Procedural Programming Languages. Retrieved Jan 22, 2016 from https://neonbrand.com/website-design/procedural-programming-vsobject-oriented-programming-a-review/ A Brief History of Object-Oriented Programming. Retrieved Jan 25, 2016 from http://web.eecs.utk.edu/~huangj/cs302s04/notes/oo-intro.html Was object-oriented programming a failure?. In Quora. Retrieved Jan 25, 2016 from https://www.quora.com/was-object-oriented-programming-a-failure Back to Basics Three or Four OOP Pillars?. Retrieved Jan 25, 2016 from http://themoderndeveloper.com/the-modern-developer/back-to-basics-three-or-four-oop-pillars/ 32 Nokia Solutions and Networks 2014