You are on page 1of 32

OOP

TEXT BOOKS :
  Java; the complete reference, 7th edition, Herbert schildt, TMH. Understanding OOP with Java, updated edition, T. Budd, pearson education.

Unit-I

Object-Oriented thinking
Need of oop paradigm / model : Object-oriented programming is a new programming model.  A model helps you understand how the system works.  For example, the Newton model of physics explains why apples fall to the ground.  In computer science , a model explains how the elements that go into a program are organized and how they interact with one another.

A Way of Viewing/Understanding the World


 To understand the major ideas in object oriented programming
 Let us see how we handle a real world situation.  Then we will see how can we make computer to model the techniques used in the situation.

Real world situation


 I want to send some flowers to my friend sally who lives in a city many miles away.  Because of the distance, I cannot pick up the flowers and carry them to sallys door myself.  I will go to Flora, my local florist, tell her the type and quantity of flowers I wish to send and sallys address.  I can be assured that the flowers will be delivered automatically.

Contd..
Gardeners Sally Grower Me Delivery person Flora Sallys Florist Flower Arranger Wholesaler

The community of agents helping me

Agents and communities


 I find an appropriate agent (Flora) and pass to her a message containing a request.  It is the responsibility of Flora to satisfy my request.  There is some method -- some algorithm or a set of operations -- used by Flora to do this.  I do not need to know the particular method that Flora will use, this information is hidden from me.

The General Principle


 An object oriented program is structured as a community of interacting agents called objects. Each object provides a service that is used by other members of the community.

Messages and Methods


 Action is initiated in object oriented programming by the transmission of a message to an agent (an object).  The message contains the request for action and is accompanied by additional information (arguments) needed to carry out the request.  The receiver is the agent to which the message is sent.  If the receiver accepts the message it accepts the responsibility to carry out the indicated action.  In response to a message the receiver will perform some method to satisfy the request.

In what sense is a message different from a procedure call?


In both cases there is a set of well-defined steps that will be executed following the request. But, there are two important differences.
 The first is that a message has a specific receiver. In a procedure call, there is no specific receiver.  The second is that the interpretation of the message ( that is, the method used to respond to the message ) is dependent on the receiver and can vary with different receivers.

Contd..
The specific receiver for any given message will not be known until run time. There is a late biding between the message and code fragment( method ) used to respond to the message. In a function or procedure call, binding name to code fragment will be done at compile time only. There is an early binding.

Classes and Instances


 A class is the template or blueprint from which objects are actually created.  All objects are instances of a class. All objects of the same class use the same method in response to the same message.

Class Hierarchies-Inheritance
The principle that knowledge of more general category is also applicable to a more specific category is called inheritance.
Employee name salary hire_date

Professor bonus

Programmer

contract_period

 Classes can be organized into a hierarchical inheritance structure. A child class (or subclass) will inherit attributes from a parent class higher in the tree.  An abstract class is a class for which there are no direct instances; it is used only to create subclass.

A class hierarchy for various material objects


Material Objects

Animal

Plant

Mammal

Flower

Dog

Platypus Human Carnation Shopkeeper Artist Dentist

Florist

Potter

Flash

Flora

Elizabeth

Kenneth

Phyl

Sallys flowers

Categories surrounding Flora

Contd..

Material Object

Flora is a florist, but Florist is a specialized form of the category Shopkeeper.

Animal

Mammal Human Shopkeeper Florist

Flora

Method Binding, Overriding and Exceptions


 Phyl the platypus presents a problem for our simple structure.  We know that mammals give birth to live babies.  But Phyl lays eggs.  To accommodate this variation, we need to find a technique to convert exceptions to a general rule.  We do this by overriding the information inherited from parent class in subclass.  Name of the method in the subclass is same as in the parent class.

 The search for a method to invoke in response to a given message starts with the class of the receiver.  If no appropriate method is found, the search continues up the parent class chain until either a method is found or the parent class chain is exhausted.

Summary of oop concepts


   Everything is an object. Computation is performed by communicating objects with each other. Objects communicate by sending and receiving messages. A message is a request for action bundled with whatever arguments may be necessary to complete the task. Each object has its own memory. Every object is an instance of a class.

 

 All objects that are instances of the same class can perform the same actions.  Classes are organized into a single rooted tree structure, called the inheritance hierarchy.  Properties of super class are automatically available to subclasses.

Dealing / Coping with Complexity


Complexity of programming projects is non-linear
Two programmers cannot do in one month what one programmer can do in 2 months Adding more men/women to a project often lengthens, not shortens, the schedule.

Interconnectedness, the dependence of one portion of code on another portion, is responsible for this situation. Abstraction mechanisms are used to deal with this complexity and abstraction layers in object oriented software design provides some assistance to reduce complexity.

Abstraction
 Abstraction is the purposeful suppression or hiding of some details of a process in order to bring out more clearly other aspects.
For example, data is stored in 512B blocks on a disk but users are presented with the higher level file abstraction. We are given variables to enter the data, but actually data will be stored in the form of 1s and 0s. Similarly header files, ex- math.h in c

Abstraction mechanisms in programming languages.


Procedures and Functions
 It increases readability of the code
 Easy to maintain the code.  Easy to find errors.

Header files and Packages


Easy to write the code. It reduces the code size. It reduces the time.

Contd.. Abstract Data Types ( classes )


 Separates interface and implementation  Easy to understand the functionality .  Provides security.

polymorphism
 Increases readability of the code.

Inheritance
 It reduces the code size .

Abstraction layers in Object-Oriented Software Design


At the highest level a program is viewed as a community of cooperating objects. At this highest level the important features to stress are the services provided by each object and the lines of communication between them. The next level of abstraction places a group of objects working together in a single unit .

Contd..

Contd..
 The next level of abstraction deals with the interaction between 2 individual objects where one uses services from the other.  The last level of abstraction considers a single task in isolation i.e. what are the steps required to implement a single method.

Programming in the Small versus Programming in the Large


Code developed by single programmer or very small set of programmers. Every individual understands all aspects of the project. Major effort is the design and development of algorithms . Code developed by a large team. No single individual is responsible for the entire project or understands all aspects of it Major effort is the management of details and the communication of information between different portions of the project.

Week 2 : a) Write a Java program that prompts the user for an integer and then prints out all prime numbers up to that integer. b) Write a Java program to multiply two given matrices. c) Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use StringTokenizer class of java.util)

Reading data from keyboard using JOptionPane


import java.lang.*; import java.io.*; import javax.swing.*; public class InputFromKeyboardJOptionPane { public static void main(String[] args) { String str=JOptionPane.showInputDialog("enter data"); System.out.println(str); } }

StringTokenizer
 StringTokenizer is a class .  It converts the input string into a set of tokens.  To use StringTokenizer, you should specify an input string and a string that contains delimiters.  Delimiters are characters that separate tokens.  The default set of delimiters consists of the whitespace characters:

The StringTokenizer constructors are shown below: StringTokenizer(String str) StringTokenizer(String str, String delimiters)

Method
boolean hasMoreTokens( )

Description
Returns true if one or more tokens remain in the string and returns false if there are none. Returns the next token as a String.

String nextToken( )

Arrays
One-Dimensional Arrays :General form

type array-var[ ] = new type[size]; Ex:- int a=new int[10]; Two-Dimensional Arrays :General form

type array-var[ ][ ] = new type[row_size][col_size];

Ex:- int

a[ ][ ]=new int[3][3];

You might also like