1. OOP vs Structured Programming

1. OOP vs Structured Programming
• Structured Programming Approach & the limitation
• Object Oriented Programming Approach


Object
  •  The two most important concepts in OOP are the class and the object.
  • Objects are the basic run time entities in our object-oriented system.
  • An object is a thing, both tangible and intangible, that we can imagine.
  • They may represent a person, a bank account, a table of data or any item that the computer must handle.
  • An object is called an instance of a class
  • It is a combination of data (variables) and code (functions) joined together into a single entity.
  • An object’s functions are referred to as methods or member functions and its data are referred to as properties or member data.
  • Calling a specific method in an object is described as sending the object a message.
  • Objects take up space in memory and have an associated address like a structure in C language.
  • When a program is executed the objects interact by sending messages to one another.
  • For example if “customer” and “account” are two objects in a program, then the customer object may send a message to the account object requesting for the bank balance.
  • Each object contains data and code to manipulate the data.


Class
  • A class is a collection of objects of similar type.
  • It is a user defined data type.
  • For example mango, apple and orange are members of the class fruit.
  • Associated with each class is a set of properties (data) and behaviors (functions).
  • A class is an essentially a description of how to make an object.
  • Every object has a class, which is used to determine how to create the object, what variables the object will contain, and what messages the object will respond to.


An example of real world OBJECT and CLASS

  • One of the main claims made for object orientation is that it is a natural way of thinking about things.
  • If we consider a CD player as an object, we can list the actions we may ask the CD player to undertake:
  • Play, pause, seek a particular track, fast forward, fast reverse, eject, etc.
  • These actions are known as methods.
  • The CD player has some state - with disk or without disk
  • The control panel shows only the information that the user is directly interested in. It does not show how the CD player actually works. We don’t want to know also. Hence most of the details of how it works are hidden from the user.
  • There are millions of such CD players in the world. Therefore we can identify a class of CD players. A particular CD Player is an object, which is an instance of a class of CD players.


2.0 ELEMENT OF OBJECT: Attribute, behavior and state

  • A real-world object, such as the registration form used to register for a course, consists of attributes and behaviors (see Figure 2.1).
  • An attribute is data associated with an object. The course name, course number, and your name and student number are examples of data associated with the registration form.
  • A behavior is something performed by an object, such as processing, modifying, or canceling a course registration.
  • A programmer’s job is to use an object-oriented programming language to translate attributes and behaviors of a real-world object into class that consists of attributes and methods understood by a computer.

Figure 2.1 Example attributes and behaviors in real world object

3.0 CHARACTERISTICS OF OOP

Abstraction

  • Class abstraction is the separation of class implementation from the use of a class.
  • The creator of a class provides a description of the class and lets the user know how the class can be used.
  • The collection of methods and fields that are accessible from outside the class, together with the description of how these members are expected to behave, serves as the class’s constant.


Figure 2.2 Class abstractions separates class implementation from the use of the class
Encapsulation

  • The wrapping of data and functions into a single unit is known as encapsulation.
  • The data is not accessible to the outside world and only those functions, which are wrapped in the class, can access it.
  • All data members (fields) of a class are declared private. Some method may be private too.
  • This insulation of data from direct access by the program is called data hiding.
  • Inheritance
  • Inheritance is a mechanism in OOP to design two or more entities that are different but share many common features.
  • Features common to all classes are defined in the superclass.
  • The classes that inherit common features from the superclass are called subclasses.
  • We also call the superclass an ancestor and the subclass a descendant.
  • An example of inheritance hierarchy among different types of birds:


Figure 2.3


  • For example, the bird robin is a part of the class FLYING BIRD that is again part of the class BIRD.
  • This inheritance concept of OOP provides the idea of reusability.
  • This means we can add additional features to an existing class with out modifying it.
  • This is possible by deriving (or extending) a new class from the existing one.
  • The new class will have the features of the old class.

Polymorphism
  • Polymorphism means the ability to take more than one form.
  • For example, an operation may exhibit different behavior in different instances.
  • The behavior depends upon the types of data used in the operation.
  • Consider the operation of addition. For two numbers, the operation will generate a sum. If the operands are strings, then the operation will produce a third string by concatenation.
  • Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface.
  • Polymorphism is implemented in the form of overloading and overriding existing methods.

Reference:

  • Liang, Y. Daniel, Introduction to Java Programming, 8th Edition, Pearson, 2011.
  • Wu, C. Thomas, An Introduction to Object Oriented Programming with Java, 4th Edition, Mc Graw Hill, 2006.