2. Outline
▪ Abstraction
▪ Structured Programming
▪ Procedural vs. Object-Oriented Programming
▪ Goals of Object-Oriented Programming
▪ Principles of Object-Oriented Programming
New Wave Analytica
3. Abstraction
▪ One of the prime concepts used to simplify programming problems
▪ Mechanism and practice to reduce and factor out details so that one can focus on
a few concepts at a time.
▪ Through the process of abstraction, a programmer hides all but the relevant data
about an object in order to reduce complexity and increase efficiency.
▪ Abstraction is one of the key concepts of object-oriented programming (OOP)
languages.
▪ Its main goal is to handle complexity by hiding unnecessary details from the user.
▪ That enables the user to implement more complex logic on top of the provided
abstraction without understanding or even thinking about all the hidden complexity.
New Wave Analytica
5. Structured Programming
▪ Further refinement of procedural
programming
▪ Formal methods of planning data flow
and functional decomposition
▪ go to is banned
6. Structured Programming
▪ A programming paradigm aimed at improving the clarity, quality, and
development time of a computer program by making extensive use of the
structured control flow constructs
▪ Selection (if/then/else)
▪ Repetition (while and for)
▪ Block structures
▪ Subroutines
▪ A programming paradigm in contrast to using simple tests and jumps such
as the go to statement.
New Wave Analytica
7. Procedural Programming
▪ A programming model which is derived from structured programming, based
upon the concept of calling procedure.
▪ Procedures, also known as routines, subroutines or functions, simply consist of
a series of computational steps to be carried out.
▪ During a program’s execution, any given procedure might be called at any point,
including other procedures or itself.
▪ Languages
▪ Basic
▪ Pascal
▪ C
New Wave Analytica
8. Procedural Programming
▪ Routines are grouped into functions
▪ A function can call another function
▪ You don’t have to understand each line, lust what each function did
▪ You could hide data to be accessible to only within a function (encapsulation)
New Wave Analytica
9. Object-Oriented Programming (OOP)
▪ Takes encapsulation even further by localizing data and associated operations
into a mini-program called an object.
▪ An object-oriented program is an ecosystem of objects that interact with each
other.
▪ “Think of an object-oriented system as a bunch of intelligent animals as objects
inside your machine, talking to each other by sending messages to one
another.” – Alllan Holub
New Wave Analytica
10. Object-Oriented Programming (OOP)
▪ A programming model which is based upon the concept of objects.
▪ Objects contain data in the form of attributes and code in the form of methods.
▪ In OOP, computer programs are designed using the concept of objects that interact with
real world.
▪ OOP languages are various but the most popular ones are class-based, meaning that
objects are instances of classes, which also determine their types.
▪ OOP takes abstraction farthest by allowing you to group related data and operations into
different types of objects.
▪ You create your own data types, which are types of objects. Each of these data types are
called objects.
New Wave Analytica
11. Object-Oriented Programming (OOP)
▪ Creating you own classes allows you to design a program so that it is
intuitive to remember how it is organized
▪Example
▪ Creating classes that represent real world entities.
▪ Creating classes to have specific responsibilities so that when you need to update a piece of code,
you know exactly where to look for it
▪ Languages
▪ Java, C++, C#, Python, PHP, JavaScript
▪ Ruby, Perl, Objective-C, Dart, Swift, Scala
New Wave Analytica
12. Goals of Object-Oriented Programming
▪ Comprehensibility
▪ Make it easier for humans to understand you code
▪ Maintainability
▪ Make code easy to modify
▪ Reusability
▪ Old code should be building blocks for new code
▪ Plugability
▪ You can create interchangeable component that can substitute for one another
just like machine parts.
New Wave Analytica
13. Class and Objects
▪ A class in Java is a blueprint for objects to follow a specific schema defined in the class.
▪ A class defines the behavior for objects of its type. It represents a collection of properties
(data and functions) for all its objects.
▪ It supports a template for creating objects which bind code and data.
▪ A class acts as a means to define methods and data.
▪It helps in maintaining access specifications for member variables using access specifiers.
New Wave Analytica
14. Class and Objects
▪ An object is the most fundamental entity in Java
▪ Objects represent real-life entities because each of them could have specific behavior,
identity, and data (attributes).
▪ In Java, the object is an offspring of its class.
▪ The class has properties to reflect the object state and methods to represent the behavior.
▪ The methods also show an object’s response to other objects. Identity is a unique name for
the object assigned by the user, much like variables.
New Wave Analytica
15. What is a Class and an Instance of a Class?
▪ Class
▪ Definition of an object
▪ Example:
▪ Your car is a 2018 BMW 3-series
▪ Instance
▪ The created object of a class
▪ Example :
▪ There are many other 2018 BMW 3-series out there but there is only one instance of your car.
New Wave Analytica
16. What is an object?
▪ Has attributes
▪ a property or components
▪ Has methods
▪ behaviours or routines
New Wave Analytica
17. Example of an Object - Car
▪ Attributes
▪ steering wheel
▪ engine
▪ color
▪ radio
▪ airconditoiner
▪ Methods
▪ go forward
▪ go backward
▪ cool the interior
▪ play music
New Wave Analytica
18. What is an interface?
▪ An object has an interface
▪ The outward appearance of an object. How other objects see the object
▪ The attributes and methods that the object exposes.
▪ Normally, an object will only expose some of its attributes and methods.
▪ Some attributes and methods are used by the object itself. Therefore, no other object
should have access to those
▪ Some attribute and methods may be made accessible only to certain other objects.
▪ Some attributes and methods may be accessible by any other object
New Wave Analytica
19. What is an interface?
▪ Normally, only methods are exposed. Objects usually hide their data to
protect then from being changed by other objects without their control
▪ Constants are an exception. These don’t change anyway
New Wave Analytica
20. Three Core Principles of OOP
▪ Encapsulation
▪ Inheritance
▪ Polymorphism
New Wave Analytica
21. Encapsulation
▪ It is a mechanism of wrapping the data (variables) and code acting on the
data (methods) together as a single unit.
▪ In encapsulation, the variables of a class will be hidden from other classes,
and can be accessed only through the methods of their current class.
▪ Grouping of data and operations into objects.
▪ Related data and operations should not be separated. They should be found
in a single object.
▪ It is also known as data hiding
New Wave Analytica
22. Encapsulation
▪To achieve encapsulation in Java
▪ Declare the variables of a class as private.
▪ Provide public setter and getter methods to modify and view the variables values.
▪ Benefits of Encapsulation
▪ The fields of a class can be made read-only or write-only
▪ A class can have total control over what is stored in its fields
▪ Simpler interfaces
▪ Only a few methods are exposed to other objects. Since interactions between objects are simple, system us easier for the
programmer to comprehend.
▪ Data protected from corruption
▪ Easier to modify code or find bugs
▪ Simpler interactions
New Wave Analytica
23. Examples of Encapsulation
▪ You shouldn’t have to ask someone with radar gun to measure the speed of
your car. Your car should have its own speedometer.
▪ Purchase Order Object
▪ Data for purchase orders should not be lumped in the same objects as data for invoices and receipts
▪ The methods for retrieving data from a purchase order should not be found in a separate class from
the data.
▪ DB Connection Object
▪ The DB Connection object should not need to look up the URL from the database from another
object every time its does an operation.
New Wave Analytica
24. Examples of Encapsulation
▪ Information Hiding
▪ AN object should expose only what is necessary, and only at the appropriate level.
▪ Think of CIA … “need-to-know”
New Wave Analytica
25. Examples of Encapsulation
▪ Car
▪ Driver : Only steering wheel, pedals, and stick shift are exposed. Driver should not access engire or
gears or axle to drive the car
▪ Mechanic
▪ Access to engine, gears, etc., but not internals of each part
▪Manufacturer
▪ Access to internals of each part
New Wave Analytica
26. Examples of Encapsulation
▪ Purchase Order Object
▪ Any object can get into from the object, but only certain objects should have authority to set
information.
▪ Only certain objects should be allowed to create PO object
▪ DB Connection Object
▪ Only the Driver Manager object can create a connection
▪ Users cannot set whether a connection is read-only or not.
New Wave Analytica
27. Inheritance
▪ It is a way to create a new class by deriving from another class.
▪ It is the mechanism in java by which one class is allowed to inherit the
features(fields and methods) of another class.
▪ Interface Inheritance
▪ The new class acquires the interface of the old class
▪ Implementation Inheritance
▪ The new class often also acquires the implementation of the old class
▪ The new class can change the implementation of the older class or ass its methods and
attributes.
New Wave Analytica
28. Inheritance
▪ Inheritance is a way to allow objects to share code, preventing code-
duplication
▪ Code supplication is the number sin in OOP
▪ Implementation inheritance is dangerous
▪ The Fragile Base Class Problem
▪ A subclass must inherit all inheritable members of the superclass
▪ No option to disinherit a member
▪ If the subclass inherits members that it doesn’t need, those members might be called by other objects in
a way that the creator of the subclass did not intend (remember you’re working in a tem) causing
undesirable behavior
29. Polymorphism
▪ When a single data type exhibits different behaviours during execution.
▪ Greek : Poly means “many”, morph means “form”
▪ Polymorphism means “existing in many forms”
▪ It is the other side of the same coin as inheritance
▪ Polymorphism is the reason why we want to have inheritance
New Wave Analytica
30. Polymorphism
▪ Polymorphism allows for “pluggability” or “substitutability”
▪ Types that implement the same interface can substitute for one another
▪ Client code just sees one class, but the actual “concrete” type can be different
in different cases.
New Wave Analytica