Download presentation
Presentation is loading. Please wait.
1
Object oriented vs procedural programming
2
Procedural programming
Although Java is primarily object oriented up until now all we have used it to produce is procedural code. The language C is an example of a strictly procedural language Object Oriented Programming(OOP) languages such as C++, C# and Java improve on this. Procedural programming is a list or set of instructions telling a computer what to do step by step and how to perform from the first code to the second code. (A systematic order of statements, functions and commands)
3
Procedural programming example code
A simple C hello world program Note the complete lack of class It has a main method And other methods could be written but that’s it #include <stdio.h> int main() { printf("Hello, World!"); return 0; }
4
Object-Oriented Concept
Object-oriented programming models the real world in terms of objects. Everything in the world can be modeled as an object. A circle is an object, a person is an object, and a Window icon is an object. Even a loan can be perceived as an object. A Java program is object-oriented because programming in Java is centered on creating objects, manipulating objects, and making objects work together.
5
Object-oriented Continued
Objects hold information about state and behaviour: States are the characteristics of the object, or the words you would use to describe it, and usually take the form of is or has descriptors. A computer is either on or off, a chair has four legs, and you have a name. Behaviours are the things the object can do, or the actions the object can perform, and are usually verbs that end in ing. You are sitting, using a computer, and reading.
6
Object-oriented Example
For Pac-Man, there are three objects: Pac-Man, a ghost, and a pac-dot. The ghost has states of: colour name state (eatable or not) direction speed and behaviours of: moving changing state
7
OO Programming example:
Properties/variables (State) Methods(Behaviour)
8
Key features of Object Oriented Programming
Abstraction Allows for simple things to represent complexity. Such as objects, classes, and variables representing more complex underlying code and data. We all know how to turn the TV on, but we don’t need to know how it works in order to enjoy it. For example in our code we could create a TV object then ask it to turn on. We don’t need to worry about how the TV object works as long as we know it has a on method within. Consider the scanner import – we don’t worry about how it works we just create a scanner object and use it when we want user input. This is important because it lets avoid repeating the same work multiple times.
9
Key features of Object Oriented Programming
Polymorphism Allows us to use the same word to mean different things in different contexts. The capability of a method to do different things and take on many forms based on the object that it is acting upon. In other words, polymorphism allows you define one interface (or class) and have multiple implementations.
10
Key features of Object Oriented Programming
Inheritance Inheritance can be defined as the process where one class acquires the properties, methods and fields of another. With the use of inheritance the information is made manageable in a hierarchical order. The class which inherits the properties of the other is known as subclass, derivedclass, childclass and the class whose properties are inherited are known as superclass, baseclass, parentclass.
11
Key features of Object Oriented Programming
Encapsulation Encapsulation in Java is a mechanism of wrapping the data variables and code acting on the data methods together as a single unit. The variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class, therefore it is also known as data hiding. To achieve encapsulation in Java we declare the variables of a class as private. Provide public setter and getter methods to modify and view the variables values. We can re-use objects like code components or variables without allowing open access to the data system-wide.
12
Object Oriented example code
class Cow { String size; String sex; public void moo() { System.out.println("Cow says moo."); } public class OldMacDonald { public static void main( String[] args ) { Cow maudine = new Cow(); An object is an instance of a class.
13
OOD vs procedural Procedural programming uses a list of instructions to tell the computer what to do step-by-step. Procedural programming relies on what’s known: Procedures: A series of computational steps to be carried out in order. Subroutines(Also know as functions/methods): Again a series of computational steps but this time they return a value. Consider how methods work in Java.
14
OOD vs procedural continued
The design method used in procedural programming is called Top Down Design. Start with a problem (procedure) Systematically break the problem down into sub problems (sub procedures). This is called functional decomposition, which continues until a sub problem is straightforward enough to be solved by the corresponding sub procedure. The difficulties with this type of programming, is that software maintenance can be difficult and time consuming. When changes are made to the main procedure (top), those changes can cascade to the sub procedures of main, and the sub-sub procedures and so on, where the change may impact all procedures in the pyramid.
15
OOD vs procedural continued
Object oriented programming is meant to address the difficulties with procedural programming. In object oriented programming, the main modules in a program are classes, rather than procedures. The object-oriented approach lets you create classes and objects that model real world objects. Procedural code is quicker to implement for simpler code but get’s increasingly complex and difficult to manage as the program get’s larger. OOP allows for easy re-use code enabling faster coding and managing on larger projects
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.