Está en la página 1de 61

Programming Languages

Third Edition

Object-Oriented Programming

Objectives
Understand the concepts of software reuse and
independence
Become familiar with the Smalltalk language
Become familiar with the Java language
Become familiar with the C++ language
Understand design issues in object-oriented
languages
Understand implementation issues in objectoriented languages
Programming Languages, Third Edition

Introduction
Object-oriented programming languages began in
the 1960s with Simula
Goals were to incorporate the notion of an object,
with properties that control its ability to react to
events in predefined ways
Factor in the development of abstract data type
mechanisms
Crucial to the development of the object paradigm
itself

Programming Languages, Third Edition

Programming Languages, Third Edition

Introduction (contd.)
By the mid-1980s, interest in object-oriented
programming exploded
Almost every language today has some form of
structured constructs

Programming Languages, Third Edition

These four requirements are satisfied


by the object-oriented paradigm.

Encapsulation Encapsulation is capturing data and keeping it


safely and securely from outside interfaces.

Inheritance- This is the process by which a class can be derived


from a base class with all features of base class and some of its
own. It allows a class to use the properties and methods of another
class. This increases code reusability.

Polymorphism- This is the ability to exist in various forms. It


allows you define one interface and have multiple
implementations.

Abstraction- The ability to represent data at a very conceptual


level without any details.

Programming Languages, Third Edition

Programming in Java is largely based on building data


types.
It revolves around the concept of an object, an entity
that holds a data type value.
With Java's primitive types we are largely confined to
programs that operate on numbers, but with reference
types we can write programs that operate on strings,
pictures, sounds, or any of hundreds of other
abstractions that are available in Java's standard
libraries

Programming Languages, Third Edition

Data types. A data type is a set of values and a set of operations on


those values.

Abstract data types. An abstract data type is a data type whose internal
representation is hidden from the client.

Objects. An object is an entity that can take on a data-type value.


Objects are characterized by three essential properties:
The state of an object is a value from its data type;
The identity of an object distinguishes one object from another;
The behavior of an object is the effect of data-type operations.

In Java, a reference is a mechanism for accessing an object.

Programming Languages, Third Edition

Abstract data type


Definition: A set of data values and associated
operations that are precisely specified independent
of any particular implementation.
Abstract data type mechanisms can increase the
independence of software components by
separating interfaces from implementations
A data type can be considered abstract when it is
defined in terms of operations on it, and its
implementation is hidden
Programming Languages, Third Edition

An abstract class is a generalization concept. It is a class you invent


to only use as a base class for inheritance but not to instantiate
objects from.

Programming Languages, Third Edition

10

Abstract data types are a mechanism that guarantees the


encapsulation and hiding of information in a clean and efficient
manner.
Abstract data types guarantee the encapsulation and hiding of
information but they are rigid when used in a design with a degree
of complexity.
They permit the encapsulation and hiding of information.
They are equipped with a mechanism which, under certain conditions,
supports the inheritance of the implementation of certain operations
from other, analogous constructs.
They are framed in a notion of compatibility defined in terms of the
operations admissible for a certain construct.
They allow the dynamic selection of operations as a function of the
effective type of the arguments to which they are applied.

Programming Languages, Third Edition

11

Software Reuse and Independence


Object-oriented programming languages satisfy
three important needs in software design:
1. Need to reuse software components as much as
possible.
2. Need to modify program behavior with minimal
changes to existing code.
3. Need to maintain the independence of different
components.

Programming Languages, Third Edition

12

Software Reuse and Independence


(contd.)
Four basic ways a software component can be
modified for reuse:
1.
2.
3.
4.

Extension of the data or operations


Redefinition of one or more of the operations
Abstraction
Polymorphism

Programming Languages, Third Edition

13

Software Reuse and Independence


(contd.)
Extension of data or operations:
Example: adding new methods to a queue to allow
elements to be removed from the rear and added to
the front, to create a double-ended queue or deque

Programming Languages, Third Edition

14

Software Reuse and Independence


(contd.)
Redefinition of one or more of the operations: (C#)
Example: a text window may have the same operations on it as
a general-purpose window, but the display operation needs
redefinition in order to display the text in the window as well as
the window itself. Similarly, if a square is obtained from a
rectangle, an area or perimeter function may need to be
redefined to take into account the reduced data needed in the
computation.
**** Operator overloading and overriding are not supported in
Java. In some other languages you can, and difference
between operator overloading and overriding is the same like
between function overloading and overwriting.
Programming Languages, Third Edition

15

Software Reuse and Independence


(contd.)
Abstraction, or collection of similar
operations from two different
components into a new component:
Example: can combine a circle and rectangle into an
abstract object called a figure, to contain the
common features of both, such as position and
movement

Programming Languages, Third Edition

16

Software Reuse and Independence


(contd.)
Polymorphism, or the extension of the type of data
that operations can apply to:
Examples: overloading and parameterized types

Application framework: a collection of related


software resources (usually in object-oriented form)
for developer use
Examples: Microsoft Foundation Classes in C++
and Swing windowing toolkit in Java

Programming Languages, Third Edition

17

Programming Languages, Third Edition

18

Programming Languages, Third Edition

19

Software Reuse and Independence


(contd.)
Object-oriented languages have another goal:
Restricting access to internal details of software
components

Mechanisms for restricting access to internal


details have several names:
Encapsulation mechanisms
Information-hiding mechanisms

Programming Languages, Third Edition

20

Smalltalk
The Smalltalk project at Xerox PARC in the 1970s
streamlined the object metaphor and drew attention
to object-oriented programming.
Although the central object-oriented concepts in
Smalltalk resemble their Simula ancestors,
Smalltalk was not an incremental modification of
any previously existing language.
It was a completely new language, with new
terminology and an original syntax.
Programming Languages, Third Edition

21

This is a list of some of the main advances of


Smalltalk over Simula:

The object metaphor was extended and refined


In Smalltalk, everything is an object, even a class.
All operations are therefore messages to objects.
Objects and classes were shown to be useful
organizing concepts for building
an entire programming environment and system.
Abstraction was added, using the distinction between
private instance variables (data associated with an
object) and
public methods (code for performing operations)
Programming Languages, Third Edition

22

These short lists, however, do not do justice to the Smalltalk project.


Smalltalk was conceived as part of an innovative and forward-looking
effort that designed an entire computing system around a new
programming language.
Smalltalk is more flexible and more powerful than Simula or other
languages that preceded it, and many Smalltalk fans consider it a
better language than most that followed.
Smalltalk is untyped, allowing greater flexibility in the modification of
programs and greater flexibility in the use of general-purpose
algorithms on a variety of data objects.
In addition, the importance of treating everything as an object cannot
be underestimated. This is the source of much of the flexibility of
Smalltalk.
Smalltalk has many interesting and powerful facilities, including a
mechanism that allows objects to detect a message they do not
understand and to respond to the message.
Programming Languages, Third Edition

23

Java
Originally intended as a programming language for
systems embedded in appliances
Emphasis was on portability and small footprint: write
once, run anywhere

Programs compile to machine-independent byte code


Provides conventional syntax, a large set of libraries,
and support for compile-time type checking not
available in Smalltalk
Is purely object-oriented, with one exception:
Scalar data types (primitive types) are not objects

Programming Languages, Third Edition

24

Programming Languages, Third Edition

25

Programming Languages, Third Edition

26

Programming Languages, Third Edition

27

Programming Languages, Third Edition

28

Basic Elements of Java: Classes,


Objects, and Methods
A Java program instantiates classes and calls
methods to make objects do things
Many classes are available in standard packages
Programmer-defined classes can be placed in their
own packages for import

Variable definition is similar to that in C:

Method call is similar to Smalltalk:


Programming Languages, Third Edition

29

Java Packages

Programming Languages, Third Edition

30

Programming Languages, Third Edition

31

Basic Elements of Java (contd.)


Example: program uses an imported class

Programming Languages, Third Edition

32

Basic Elements of Java (contd.)


Java Virtual Machine runs the program as
TextComplex.main(<array of strings>)
Command-line arguments present at launch are
placed into the args array

All classes inherit from the Object class by default


Data encapsulation is enforced by declaring
instance variables with private access
They are visible only within the class definition

Programming Languages, Third Edition

33

Basic Elements of Java (contd.)


When a number of objects are created from the same
class blueprint, they each have their own distinct copies
of instance variables.
In the case of the Bicycle class, the instance variables are
cadence, gear, and speed. Each Bicycle object has its
own values for these variables, stored in different memory
locations.
Sometimes, you want to have variables that are common
to all objects. This is accomplished with the static modifier.

Programming Languages, Third Edition

34

Basic Elements of Java (contd.)


Fields that have the static modifier in their declaration are
called static fields or class variables.
They are associated with the class, rather than with any
object.
Every instance of the class shares a class variable, which
is in one fixed location in memory.
Any object can change the value of a class variable, but
class variables can also be manipulated without creating
an instance of the class.

Accessor methods: allow programs to view but


not modify the internal state of a class.
Programming Languages, Third Edition

35

Basic Elements of Java (contd.)


Constructors: like methods, they specify initial
values for instance variables and perform other
initialization actions
Default constructor: has no parameters
Constructor chaining: when one constructor calls
another

Use of private access to instance variables and


accessor methods allows us to change data
representation without disturbing other code
Java uses reference semantics
Classes are also called reference types
Programming Languages, Third Edition

36

Explanation:In the above class,you are not


creating any constructor so compiler provides you
a default constructor.Here 0 and null values are
provided by default constructor.
Programming Languages, Third Edition

37

Basic Elements of Java (contd.)


== is the equality operator for primitive types, and
also means object identity for reference types
Object class contains an equals method that can be

overridden in subclasses to implement a comparison of


two distinct objects

Methods are invoked after instantiation using dot


notation:
Can nest operations:
Java does not allow operator overloading like C++
Java does not allow multimethods, in which more
than one object can be the target of a method call
Programming Languages, Third Edition

38

Programming Languages, Third Edition

39

Programming Languages, Third Edition

40

Programming Languages, Third Edition

41

The Java Collection Framework:


Interfaces, Inheritance, and
Polymorphism
Framework: a collection of related classes
java.util package: contains the Java collection
framework
Interface: a set of operations on objects of a given
type
Serves as the glue that connects components in
systems
Contains only type name and a set of public method
headers; implementer must include the code to
perform the operations
Programming Languages, Third Edition

42

The Java Collection Framework


(contd.)

Programming Languages, Third Edition

43

The Java Collection Framework


(contd.)

Programming Languages, Third Edition

44

The Java Collection Framework


(contd.)
<E> and E in the interface and method headers are

type parameters
Java is statically typed: all data types must be
explicitly specified at compile time
Generic collections: exploit parametric
polymorphism
Raw collections in early versions of Java did not

Examples:

Programming Languages, Third Edition

45

Programming Languages, Third Edition

46

The Java Collection Framework


(contd.)
Some classes, such as LinkedList, implement
more than one interface
A linked list can behave as a list or as a queue

Same methods can be called on the two List


variables even though they have different
implementations and different element types
Can only call Queue interface methods on the
queueOfFloats variable because it is type Queue
Programming Languages, Third Edition

47

The Java Collection Framework


(contd.)
Example: develop a new type of stack class called
LinkedStack, as a subclass of the
AbstractCollection class
Gives us a great deal of additional behavior for free

Private inner class: a class defined within another


class
No classes outside of the containing class need to
use it

Java Iterable interface only contains the


iterator method
Programming Languages, Third Edition

48

The Java Collection Framework


(contd.)

Programming Languages, Third Edition

49

The Java Collection Framework


(contd.)
Iterator type is parameterized for the same

element type as its backing store


Is an interface that specifies three methods

To visit each element in the backing store, use


hasNext and next methods
Programming Languages, Third Edition

50

The Java Collection Framework


(contd.)
Javas enhanced for loop is syntactic sugar for the
iterator-based while loop
Only prerequisites to use this: the collection class
must implement the Iterable interface and
implement an iterator method

Example:

Programming Languages, Third Edition

51

Dynamic Binding and Static Binding


Static binding: process of determining at compile
time which implementation of a method to use by
determining the objects actual class
Actual code is not generated by the compiler unless
the method is declared as final or static

When Java cannot determine the objects method


at compile time, dynamic binding is used
Java uses a jump table, which is more efficient
than a search of an inheritance tree to perform
dynamic binding
Programming Languages, Third Edition

52

Static binding is a binding which happens during compilation. It is also


called early binding because binding happens before a program actually runs.

Programming Languages, Third Edition

53

Static binding is a binding which happens during compilation. It is also


called early binding because binding happens before a program actually runs.

Programming Languages, Third Edition

54

Dynamic binding is a binding which happens during run time. It is also


called late binding because binding happens when program actually is
running.

Programming Languages, Third Edition

55

Programming Languages, Third Edition

56

Java - Polymorphism

Polymorphism is the ability of an object to take on many forms. The most


common use of polymorphism in OOP occurs when a parent class
reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered to be
polymorphic. In Java, all Java objects are polymorphic since any object will
pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is
through a reference variable. A reference variable can be of only one type.
Once declared, the type of a reference variable cannot be changed.
The reference variable can be reassigned to other objects provided that it
is not declared final. The type of the reference variable would determine
the methods that it can invoke on the object.
A reference variable can refer to any object of its declared type or any
subtype of its declared type. A reference variable can be declared as a
class or interface type.

Programming Languages, Third Edition

57

Programming Languages, Third Edition

58

Polymorphism
Java has two kinds of polymorphism:
Overloading : Static
Subtype : dynamic:

59
K. Louden, Programming Languages

Programming Languages, Third Edition

60

Design Issues
in Object-Oriented Languages
Object-oriented features represent dynamic rather
than static capabilities
Must introduce features in a way to reduce the
runtime penalty of the extra flexibility

Inline functions are an efficiency in C++


Other issues for object-oriented languages are the
proper organization of the runtime environment and
the ability of a translator to discover optimizations
Design of the program itself is important to gain
maximum advantage of an object-oriented language
Programming Languages, Third Edition

61

También podría gustarte