Jerry KotubaSYST39409-Object Oriented Methodologies1 Object Oriented Methodologies Week04.
Object Oriented Programming - Team Z – Zuniga team web...
Transcript of Object Oriented Programming - Team Z – Zuniga team web...
1 © Nokia Solutions and Networks 2014
<Change information classification in footer>
Object Oriented
Programming
•Ray John Pamillo
•1/27/2016
2 © Nokia Solutions and Networks 2014
<Change information classification in footer>
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
3 © 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++
<Change information classification in footer>
5 © Nokia Solutions and Networks 2014
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.
<Change information classification in footer>
function doTheMostExpressiveThingPossible(){
// do something expressive here
}
7 © Nokia Solutions and Networks 2014
Object Oriented 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
<Change information classification in footer>
Procedural Programming
- 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.
9 © Nokia Solutions and Networks 2014
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”
<Change information classification in footer>
10 © Nokia Solutions and Networks 2014
Real-world Objects
- They all have STATE and BEHAVIOR.
<Change information classification in footer>
Software Objects
- Consists of state and behavior.
- Stores its state in FIELDS (or variables) and exposes its behavior through
METHODS (or functions).
11 © Nokia Solutions and Networks 2014
Benefits of bundling code into objects…
• Modularity
• Information-hiding
• Code re-use
• Pluggability and debugging ease
<Change information classification in footer>
12 © Nokia Solutions and Networks 2014
Object
- An instance of a class.
<Change information classification in footer>
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.
13 © Nokia Solutions and Networks 2014
<Change information classification in footer>
Object
Person objectPerson = new Person();
Class
class Person {
}
14 © Nokia Solutions and Networks 2014
The Class as a “Blueprint” is composed of …
<Change information classification in footer>
• 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.
15 © Nokia Solutions and Networks 2014
Components of a Class
<Change information classification in footer>
• Instance Variables
• Methods
– the set of variables that the object will possess.
– the set of operations that the object will provide.
– functions
– hold the state of a particular object
16 © Nokia Solutions and Networks 2014
A Class example in Java…
<Change information classification in footer>
public class Person{
private String name;
private int age;
public Person(){
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
17 © Nokia Solutions and Networks 2014
Things to remember:
<Change information classification in footer>
DRY
Don’t Repeat Yourself
KISS
Keep It Simple, Stupid
18 © Nokia Solutions and Networks 2014
<Change information classification in footer>
The 4 Pillars of OOP
19 © Nokia Solutions and Networks 2014
The 4 Pillars of OOP
• Abstraction
<Change information classification in footer>
• Encapsulation
• Inheritance
• Polymorphism
20 © 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.
<Change information classification in footer>
21 © Nokia Solutions and Networks 2014
Abstraction
• The “abstract” keyword.
• Use of methods without body / implementation.
<Change information classification in footer>
22 © 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”
<Change information classification in footer>
23 © 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
<Change information classification in footer>
24 © 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
<Change information classification in footer>
25 © Nokia Solutions and Networks 2014
Inheritance
• Use of the “extends” keyword
• Method overriding
<Change information classification in footer>
26 © 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.
<Change information classification in footer>
27 © 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
<Change information classification in footer>
28 © Nokia Solutions and Networks 2014
Interfaces : Examples
<Change information classification in footer>
public interface Vehicle {
void start();
void turn(String direction);
}
public interface Flight {
int getAltitude();
}
29 © Nokia Solutions and Networks 2014
Programming to an Interface
<Change information classification in footer>
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;
}
}
30 © Nokia Solutions and Networks 2014
Programming to an Interface
<Change information classification in footer>
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”);
}
}
}
32 © Nokia Solutions and Networks 2014
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-vs-
object-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/
<Change information classification in footer>