Object Oriented Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Object Oriented Programming

Description:

Object Oriented Programming By: Vinod Kumar * * Java Source code Java Compiler Java enabled Web Browser Java Interpreter Output Output Two ways of using Java Java ... – PowerPoint PPT presentation

Number of Views:359
Avg rating:3.0/5.0
Slides: 55
Provided by: STD95
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Programming


1
Object Oriented Programming
  • By Vinod Kumar

2
Contents
  • Comparison of POL and OOL
  • Basics of OOP
  • Objects Classes
  • Classes with Methods
  • Features of OOP
  • Applications of OOP
  • Introduction to JAVA
  • Operator
  • Data Type Variables

3
Why OOP ?
Well! Let me identify the procedures I need.
After all what is a program if not a list of
procedures
Well! Let me identify the key players. They can
be the classes of my code
The shape
OO programmer
procedural programmer
Here's the code for the classes
Here is my code with rotate playSound as the
procedures
Square
rotate() //code playSound() //code
Circle
rotate() //code playSound() //code
rotate(shapenum) //make the shape rotate
360 playSound(shapenum) //use shapenum to
find which AIF to play
Triangle
rotate() //code playSound() //code
4
OOPS!!! The spec has changedWho is more
affected???
OOl!! I just have to write one more class I
dont have to touch the code already written
Hmm! The playSound code has to be changedwhich
means I will have to rewrite the code every time
the spec changes
The modification in the shape
Amoeba
rotate() //code playSound() //code to play .hif file
playSound(shapenum) //if the shape is not
amoeba, use shapenum to //find which AIF
to play else //play amoeba.hif sound
Oh Great!! How efficient OOP is with so many
features.Flexibility, extensibility.
5
Procedure Oriented vs Object Oriented Languages
  • POL (COBOL, PASCAL C etc.)
  • Emphasis is on doing things (algorithms)
  • Large programs are divided into smaller programs
    known as functions.
  • Most of the functions share global data
  • Data move openly around the system from function
    to function.
  • Functions transform data from one from to
    another.
  • Follows top-down approach in program design.
  • OOL (C, JAVA C etc.)
  • Emphasis is on data rather than procedure.
  • Programs are divided into what are known as
    objects.
  • Functions that operate on the data of an object
    are tied together in the data structure.
  • Data is hidden and can not be accessed by
    external functions.
  • Objects may communicate with each other through
    functions.
  • New data and functions can be easily added
    whenever necessary.
  • Follows bottom-up approach in program design.

6
Objects
  • Objects are the basic runtime entities in OOS.
  • Objects have a unique name (within a class) and
    have some responsibilities .
  • Objects communicate to other objects by sending
    messages.
  • Messages are received by the methods of an object.
  • An object is like a black box.
  • The internal details are hidden.

7
Objects cont..
  • Each object contains data and code to manipulate
    the data.
  • Objects can interact without having to know the
    details of each others data or code.
  • It is sufficient to know the type of message
    accepted and the type of response returned by the
    objects.

8
Representation of an Object
  • Object
  • Data
  • Methods

Person Name Basic Pay Salary() Tax()
9
What may be the objects?
  • Tangible Things as a car, printer, ...
  • Roles as employee, boss, ...
  • Incidents as flight, overflow, ...
  • Interactions as contract, sale, ...
  • Specifications as colour, shape,

10
So, what are objects? (Definition)
  • an object represents an individual, identifiable
    item, unit, or entity, either real or abstract,
    with a well-defined role in the problem domain.
  • Or
  • An "object" is anything to which a concept
    applies.
  • Etc.

11
Why do we care about objects? (advantages)
  • Modularity - large software projects can be split
    up in smaller pieces.
  • Reuseability - Programs can be assembled from
    pre-written software components.
  • Extensibility - New software components can be
    written or developed from existing ones.

12
Class
  • The entire set of data and code of an object can
    be made a user-defined data type using the
    concept of a class.
  • A class may be thought as a data type and an
    object as a variable of that data type.
  • So a class is a collection of objects of similar
    type.
  • Classes may have any no. of objects.
  • Classes are user defined data types and behave
    like the built-in types of a programming
    language.

13
Examples
  • Mango, apple and orange are members of the class
    fruit.
  • Class fruit
  • int weight
  • int price
  • void getdata(int x, int y)
  • weightx
  • pricey
  • int multi_wp()
  • int wp
  • wpweightprice
  • return(wp)

Creating an object fruit mangonew
fruit() fruit applenew fruit()
14
Java program
class Rectangle int length, width void
getdata(int x, int y) lengthx
widthy int rectArea()
int area arealengthwidth
return(area)
class RectArea public static void main(String
args) int area1,area2 Rectangle
rect1new rectangle() Rectangle rect2new
rectangle() rect1.length15
rect1.width10 area1rect1.lengthrect1.width
rect2.length20 rect2.width12
area2rect2.lengthrect2.width
System.out.println(Area1area1)
System.out.println(Area1area1)
15
Basic concepts of OOP
  • Data Abstraction and Encapsulation
  • Inheritance
  • Polymorphism
  • Dynamic Binding
  • Message Communication

16
Data Abstraction Encapsulation
  • Information in
    Information out
  • fig EncapsulationObjects as black
    boxes

Data Method
17
Definition
  • The wrapping up of data and methods into a single
    unit (called class) is known as encapsulation.
    The data is not accessible to the outside worlds
    and only those methods, which are wrapped in the
    class, can access it (data hiding).
  • Abstraction refers to the act of representing
    essential features without including the
    background details or explanations.

18
Inheritance
  • Inheritance means that one class inherits the
    characteristics of another class.This is also
    called a is a relationship
  • Inheritance is the process by which objects of
    one class acquire the properties of objects of
    another class.
  • Inheritance supports the concept of hierarchical
    classification.
  • In OOP the concept of inheritance provides the
    idea of reusability i.e. we can add additional
    features to an existing features to an existing
    class without modifying it. This is possible by
    deriving a new class from the existing one. The
    new class will have the combined features of both
    the classes.

19
Birds Feathers Lay eggs
Nonflying Bird Attributes
Flying Bird Attributes
Kiwi Attributes
Swallow Attributes
Parrot Attributes
Penguin Attributes
20
Polymorphism
  • Polymorphism means having many forms. It allows
    different objects to respond to the same message
    in different ways, the response specific to the
    type of the object.
  • Polymorphism means the ability to take more than
    one form.
  • Polymorphism plays an important role in allowing
    objects having different internal structures to
    share the same external interface.
  • Polymorphism is extensively used in implementing
    inheritance.

21
Example (Polymorphism)
Shape Draw()
Square Draw()
Circle Draw()
Rectangle Draw()
22
Dynamic Binding
  • Binding refers to the linking of a procedure call
    to the code to be executed in response to the
    call.
  • Dynamic binding means that the code associated
    with a given procedure call is not known until
    the time of the call at runtime.
  • It is associated with polymorphism and
    inheritance.

23
Message Communication
  • The most important aspect of an object is its
    behaviour (the things it can do). A behaviour is
    initiated by sending a message to the object
    (usually by calling a method).

24
Object1
Object3
Object2
Object4
Object5
25
The two steps of Object Oriented Programming
  • Making Classes Creating, extending or reusing
    abstract data types.
  • Making Objects interact Creating objects from
    abstract data types and defining their
    relationships.

26
Features of OOPs
  • Through inheritance, we can eliminate redundant
    code and extend the use of existing classes.
  • The principle of data hiding helps the programmer
    to build secure programs.
  • Possibility of multiple objects.
  • Easy to partition any project-work in objects /
    classes
  • OOS can be easily upgraded from small to large
    systems.
  • S/w complexity can be easily managed.

27
Applications of OOP
  • Real time systems
  • Object oriented databases
  • Hypertext hypermedia and expert text
  • AI and expert systems
  • Neural networks and parallel programming
  • Decision support and office automation system

28
Introduction to JAVA
  • James Gosling and Sun Microsystems USA (1991)
  • Oak
  • Java, May 20, 1995, Sun World
  • HotJava
  • The first Java-enabled Web browser
  • JDK Evolutions (1997)
  • J2SE, J2ME, and J2EE (not mentioned in the book,
    but could discuss here optionally)
  • In 2004, J2SE with JDK 5.0 (instead of JDK 1.5)
    was released. This is known as J2SE5.0
  • In 2008, J2SE with JDK 6.0 (JDK 1.6) was
    released. This is known as J2SE6.0

29
Characteristics of Java
  • Java is architecture-neutral
  • Java is portable
  • Javas performance
  • Java is multithreaded
  • Java is dynamic
  • Extensible
  • Java is simple
  • Java is object-oriented
  • Java is distributed
  • Java is compiled
  • interpreted
  • Java is robust secure

30
JDK Editions
  • Java Standard Edition (J2SE)
  • J2SE can be used to develop client-side
    standalone applications or applets.
  • Java Enterprise Edition (J2EE)
  • J2EE can be used to develop server-side
    applications such as Java servlets and Java
    ServerPages.
  • Java Micro Edition (J2ME).
  • J2ME can be used to develop applications for
    mobile devices such as cell phones.

31
Differences between Java C
  • Java does not support operator overloading.
  • The operator can be used to concatenate
    strings.
  • Java does not have a header preprocessor such
    as include or define
  • Java does not support multiple inheritance of
    classes. This is accomplished using a new feature
    called interface.
  • Java does not support Global variables.
  • Java does not use pointers.
  • Java has replaced the destructor with a
    finalize() function.
  • No nested classes in java.
  • Java support multithreading.
  • Constants can be created using the final modifier
    when declaring class and instance variables.

32
Java Internet
Remote Computer

Internet
Applet
Local Computer
33
Programming with Java
  • Java is a general purpose, object oriented
    programming language.
  • We can develop two types of Java programs
  • Stand alone applications.
  • Web applets

34
Two ways of using Java
Java Source code
Java Compiler
Application type
Applet type
Java enabled Web Browser
Java Interpreter
Output
Output
35
Java Program Structure
Documentation Section Package Statement Import
statement Interface Statement Class Definition
Main method Class main method
Definition
Essential
36
Java Tokens
  • A java program is basically a collection of
    classes.
  • A class is defined by a set of declaration
    statements and methods containing executable
    statements. Most statements contain expression
    which describes the actions carried out on data.
  • The smallest individual units in a program are
    known as tokens.
  • Java language includes five types of tokens
  • Reserved keywords
  • Identifiers
  • Literals
  • Operators
  • Separators

37
Implementing a Java program
  • Implementation of a Java application program
    involves following three steps
  • creating the program
  • compiling the program
  • running the program
  • We take a very simple program
  • class test
  • public static void main (String args)
  • System.out.println(Hello)
  • System.out.println(I am Java)
  • System.out.println(I am very simple)
  • Saving this file
  • test.java
  • (file name must be the class-name of
  • the class containing the main
  • method. This file is called the source
  • file.)
  • Compiling the program
  • javac test.java
  • (javac compiler creates a file called
  • test.class containg the bytes
  • codes of the program.)
  • Running the program
  • Java test
  • (the interpreter looks for the main method
  • in the program and begins from there.)

38
Constants, Variables Data Types
  • Constants
  • Constants in Java refer to fixed values that do
    not change during the execution of a program
  • Declaration of constants in Java
  • final datatype CONSTANTNAME VALUE
  • final double PI 3.14159
  • final int SIZE 3

39
  • Variables
  • A variable is an identifier that denotes a
    storage location used to store a data value.
    Unlike constants, a variable may take different
    values at different times during the execution of
    the program.
  • Examples
  • average marks
  • total_money
  • Note
  • TOTAL and total are not same.
  • 8avg is not allowed.
  • Class is not allowed.
  • avg length is not allowed.

40
  • Data Types
  • Every variable in Java has a data type. Data
    types specify the size and type of values that
    can be stored.

Data types
Primitive (Intrinsic)
Non Primitive (Derived)
Numeric
Non-Numeric
Classes
Arrays
Interface
Boolean
Character
Integer
Floating pt.
41
Variables
  • // Compute the first area
  • radius 1.0
  • area radiusradius3.14159
  • System.out.println("The area is area " for
    radius "radius)
  • // Compute the second area
  • radius 2.0
  • area radiusradius3.14159
  • System.out.println("The area is area " for
    radius "radius)

42
Integer
Long
Byte
Short
int
Floating point
Float
Double
43
Numerical Data Types
  • byte 8 bits (1 byte)
  • short 16 bits (2 bytes)
  • int 32 bits (4 bytes)
  • long 64 bits (8 bytes)
  • float 32 bits (4 bytes)
  • double 64 bits (8 bytes)

44
Declaring Variables
  • int x // Declare x to be an
  • // integer variable
  • double radius // Declare radius to
  • // be a double variable
  • char a // Declare a to be a
  • // character variable

45
Assignment Statements
  • x 1 // Assign 1 to x
  • radius 1.0 // Assign 1.0 to radius
  • a 'A' // Assign 'A' to a

46
Few points for Integer Floating point
  • It should be remembered that wider data types
    require more time for manipulation and therefore
    it is advisable to use smaller data types,
    wherever possible (this will improve the speed of
    execution of the program)
  • Float type numbers------single precision numbers
  • Double type numbers------double precision numbers
  • Floating point numbers are treated as double
    precision numbers.
  • To force them to be in single precision mode, we
    must append f or F to the numbersExample
  • 23f
  • 56933e5F
  • double d 100.2d ordouble d
    100.2D

47
Declaring and Initializingin One Step
  • int x 1
  • double d 1.4
  • float f 1.4
  • Is this statement correct?

48
Number Literals
  • int i 34
  • long l 1000000
  • float f 100.2f orfloat f 100.2F
  • double d 100.2d ordouble d 100.2D

49
Operators
  • , -, , /, and
  • 5/2 yields an integer 2.
  • 5.0/2 yields a double value 2.5
  • 5 2 yields 1 (the remainder of the division)

50
Shortcut Operators
Operator Example Equivalent i8 i
i8 - f-8.0 f f-8.0 i8 i i8 / i/8 i
i/8 i8 i i8
51
Increment andDecrement Operators
  • x 1
  • y 1 x
  • y 1 x
  • y 1 x--
  • y 1 --x

52
Numeric Type Conversion
  • Consider the following statements
  • byte i 100
  • long l i34
  • double d i3.1l/2
  • int x l (Wrong)
  • long l x (fine, implicit casting)

53
Type Casting
  • double
  • float
  • long
  • int
  • short
  • byte

54
Type Casting, cont.
  • Implicit casting
  • double d 3 (type widening)
  • Explicit casting
  • int i (int)3.0 (type narrowing)
  • What is wrong? int x 5/2.0
Write a Comment
User Comments (0)
About PowerShow.com