Вы находитесь на странице: 1из 22

Object Oriented Programming

in Java
Procedural vs. OO PROGRAMMING

Procedural PROGRAMMING:
MAIN PROGRAM GLOBAL DATA

FUNCTION 1 FUNCTION 2 FUNCTION 3

FUNCTION 4 FUNCTION 5

2
Procedural Programming

 Using function
 Function & program is divided into modules
 Every module has its own data and function
which can be called by other modules.

3
Drawbacks of Procedural
programming:
 No restriction on global data.
 No mechanism to represent relationship
between global data and functions of
the program through a single name,
which can show the relationship of the
global data and functions.

4
OBJECT ORIENTED PROGRAMMING

Object 2
Object 1

Data Data

Function Function

Object 3

Data

Function

5
OBJECT ORIENTED PROGRAMMING

Objects have both data and methods


Objects of the same class have the same data
elements and methods
Objects send and receive messages to invoke actions

Key idea in object-oriented:

The real world can be accurately described as a collection of


objects that interact.

6
Basic terminology

object
- usually a person, place or thing (a noun)
method
- an action performed by an object (a verb)
attribute
- description of objects in a class
class
- a category of similar objects (such as automobiles)
- does not hold any values of the object’s attributes
7
Example for attributes and methods
Attributes: Methods:
 manufacturer’s  Define data items
name (specify
 model name manufacturer’s
 year made name, model,
year, etc.)
 color
 Change a data
 number of doors
item (color,
 size of engine
engine, etc.)
 etc.
 Display data items

 Calculate cost

 etc.
8
Why OOP?

Save development time (and cost) by reusing code


once an object class is created it can be used in

other applications
Easier debugging
classes can be tested independently

reused objects have already been tested

9
Design Principles of OOP

Four main design principles of Object-Oriented


Programming(OOP):

Encapsulation
Abstraction
Polymorphism
Inheritance

10
Encapsulation

Also known as data hiding


Only object’s methods can modify information in
the object.

Analogy:
ATM machine can only update accounts of one
person or object only.

11
Abstraction
 Focus only on the important facts about the
problem at hand
 to design, produce, and describe so that it
can be easily used without knowing the
details of how it works.
Analogy:
 When you drive a car, you don’t have to
know how the gasoline and air are mixed and
ignited.
 Instead you only have to know how to use
the controls.
 Draw map

12
Polymorphism

the same word or phrase can mean different


things in different contexts

Analogy:
In English, bank can mean side of a river or a
place to put money
move -

13
Function Overloading

 The operation of one function depends on


the argument passed to it.
 Example: Fly(), Fly(low), Fly(150)

14
Inheritance

Inheritance—a way of organizing classes


Term comes from inheritance of traits like eye
color, hair color, and so on.
Classes with properties in common can be
grouped so that their common properties are only
defined once.
Superclass – inherit its attributes & methods to
the subclass(es).
Subclass – can inherit all its superclass attributes
& methods besides having its own unique
attributes & methods. 15
An Inheritance Hierarchy

Superclass
Vehicle
Subclasses

Automobile Motorcycle Bus

Sedan Sports Car Luxury Bus School Bus

What properties does each vehicle inherit from the types of vehicles above it
in the diagram?

16
Object-Oriented Programming Languages

 Pure OO Languages
Smalltalk, Eiffel, Actor, Java

 Hybrid OO Languages
C++, Objective-C, Object-Pascal

17
Review: Introduction to Object
Orientation
 What are the four basic principles of object
orientation? Provide a brief description of
each.
 What is an Object and what is a Class? What
is the difference between them?
 What is an Attribute?
 What is an Operation?
 What is inheritance?
 What is polymorphism?
 Describe the strengths of object orientation.

18
Class & Object
 Class is logical structure.
 Object is a physical entity.
 A class defines a new data type.
 A class is the template of its objects.
 A class encapsulates the members of the object.
 A class can have two types of members.
 Data Members
 Member functions
 Global variables declared inside the class are called
data members.
Class & Object
 The functions defined in side the class
are called member functions.
 All the members of a class are
accessible to the object of the class.
 An object can access any of it members
through dot(.) operator.
Class & Object
 Data members are also called instance
variable.
 When ever an object of the class is created a
separate copy of the data members is
created.
 Each object has a unique copy of data
members.
 Member functions are created once in
memory.
Example
1. class Example 1. class demo
2. { 2. {
3. int x,y; // Data Members 3. public static void main(String as[])
4. void set(int a, int b) //Member 4. {
function 5. //creating object using new key
5. { word
6. x=a; 6. Example Ob;
7. y=b; 7. Ob =new Example();
8. } 8. // access member function
9. void show() //Member function 9. Ob.set(10,20);
10. { 10. Ob.show();
11. System.out.println(“x=“+x+”y=“+y) 11. //setting the values of data
; members directly.
12. } 12. Ob.x=5;
13. } 13. Ob.y=8;
14. Ob.show();
15. }
16. }

Вам также может понравиться