chap1_introduction to oop

36
8/12/2019 Chap1_Introduction to OOP http://slidepdf.com/reader/full/chap1introduction-to-oop 1/36

Upload: miniscribdlogin

Post on 03-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 1/36

Page 2: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 2/36

Chapter 1

Page 3: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 3/36

What is Object?  An object is defined by two terms: attributes and

behaviors.

 A person has attributes, such as eye color, age, height,and so on. A person also has behaviors, such as

 walking, talking, breathing, and so on. In its basic definition, an object is an entity that

contains both data and behavior.

The word both is the key difference between OO

programming and other programming methodologies.

Page 4: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 4/36

Procedural Versus OO Programming

Procedure ObjectDivided Into  In POP, program is divided into

small parts called functions.In OOP, program is divided intoparts called objects.

ImportanceIn POP, Importance is notgiven to data but to functions

as well as sequence of actionsto be done.

In OOP, Importance is given tothe data rather than procedures

or functions because it works asareal world.

 Approach POP follows Top Downapproach.

OOP follows Bottom Upapproach.

 Access Specifiers POP does not have any accessspecifier.

OOP has access specifiers

named Public, Private,Protected, etc.

Data Moving In POP, Data can move freelyfrom function to function inthe system.

In OOP, objects can move andcommunicate with each otherthrough member functions.

Page 5: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 5/36

Procedure Object

Expansion To add new data and function inPOP is not so easy.

OOP provides an easy wayto add new data and

function.

Data Access 

In POP, Most function usesGlobal data for sharing that canbe accessed freely from functionto function in the system.

In OOP, data can not moveeasily from function tofunction, it can be keptpublic or private so we cancontrol the access of data.

Data Hiding POP does not have any proper way for hiding data so it is lesssecure.

OOP provides Data Hidingso provides more security .

Overloading In POP, Overloading is notpossible.

In OOP, overloading ispossible in the form ofFunction Overloading andOperator Overloading.

Examples Example of POP are : C, VB,FORTRAN, Pascal.

Example of OOP are : C++, JAVA, VB.NET, C#.NET.

Page 6: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 6/36

Approaches Top –Down approach :

Say for example, you want to use any variable in yourprogram so that must be declared in top section then &then you can use it in bottom (anywhere in program ) so

to access any variable in bottom (anywhere in program ) you have to declare that variable in top section of theprogram in POP. That’s why it is Top Down approach ,

 you can not declare variable anywhere in program.

Bottom Up approach :

Here there is no need to go in top section to declare variable, you can declare variable in top section as wellas anywhere in program, just declare it before using. 

Page 7: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 7/36

Using Global Data

Page 8: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 8/36

Terminologies Proper Design

 We can state that when properly designed, there is nosuch thing as global data in an OO model. This factprovides a high amount of data integrity in OO systems.

Data Hiding In OO terminology, data is referred to as attributes, and

behaviors are referred to as methods. Restricting accessto certain attributes and/or methods is called datahiding.

Encapsulation

By combining the attributes and methods in the sameentity.

Page 9: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 9/36

Delve with Object What Exactly Is an Object?

Objects are the building blocks of an OO program. Aprogram that uses OO technology is basically acollection of objects.

Object Data

The data stored within an object represents the state ofthe object.

In OO programming terminology, this data is calledattributes.

Object Behaviors The behavior of an object is what the object can do.

  In OO programming terminology these behaviors arecontained in methods, and you invoke a method bysending a message to it.

Page 10: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 10/36

The following information is all the user needs to

know to effectively use the methods: The name of the method

The parameters passed to the method

The return type of the method

Page 11: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 11/36

Each class diagram is defined by three separate

sections: the name itself, the data (attributes), and thebehaviors (methods).

UML Class Diagrams

Modeling Tools

What is Class Diagram?

Page 12: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 12/36

Page 13: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 13/36

What Exactly Is a Class? A class is a blueprint for an object.

It is difficult to describe a class without using the termobject and visa versa.

Classes Are Object Templates.

 A class is used to create an object. A class defines the attributes and behaviors that all

objects created with this class will possess.

Page 14: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 14/36

 Attributes

Each class must define the attributes that will store thestate of each object instantiated from that class.

Methods

Methods implement the required behavior of a class.

Every object instantiated from this class has the

methods as defined by the class. Methods may implement behaviors that are called from

other objects (messages) or provide the fundamental,internal behavior of the class.

Messages Messages are the communication mechanism between

objects.

Page 15: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 15/36

Using UML to Model a Class Diagram The person class diagram.

Getter and Setter

Page 16: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 16/36

Encapsulation and Data Hiding One of the primary advantages of using objects is that

the object need not reveal all its at-tributes andbehaviors.

Encapsulation is defined by the fact that objects

contain both the attributes and behaviors. Data hiding is a major part of encapsulation.

Interfaces

Interface defines the fundamental means of

communication between objects. The interface should completely describe how users of

the class interact with the class.

In most OO languages, the methods that are part of the

interface are designated as public. 

Page 17: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 17/36

Private Data

For data hiding to work, all attributes should bedeclared as private.

Thus, attributes are never part of the interface. Onlythe public methods are part of the class interface.

Declaring an attribute as public breaks the concept ofdata hiding.

Implementations

Only the public attributes and methods are considered

the interface. The user should not see any part of the

implementation.

Page 18: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 18/36

 A Real-World Example of theinterface/Implementation Paradigm.????

A M d l f th I t f /I l t ti

Page 19: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 19/36

A Model of the Interface/Implementation

Paradigm. 

Page 20: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 20/36

Page 21: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 21/36

Page 22: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 22/36

This allows creation of brand new classes byabstracting out common attributes and behaviors.

One of the major design issues in OO programming isto factor out commonality of the various classes.

Page 23: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 23/36

Superclasses and Subclasses:

The superclass, or parent class, contains all theattributes and behaviors that are common to classes thatinherit from it.

The class which inherits those common features knownas child class or sub class.

 Abstraction 

In most recent OO languages (such as Java and .NET), aclass can only have a single parent class; however, a classcan have many child classes.

Some languages, such as C++,can have multiple parents.

The former case is called single-inheritance, and thelatter is called multiple-inheritance.

Page 24: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 24/36

Page 25: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 25/36

Page 26: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 26/36

Is-a Relationships

 When subclass directly inherits from super class, thisrelation ship called “Is –a Relationship” .

 When a subclass inherits from a superclass, it can doanything that the superclass can do.

Page 27: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 27/36

Polymorphism Polymorphism is a Greek word that literally means

many shapes or forms.

polymorphism is tightly coupled to inheritance, it isoften cited separately as one of the most powerful ad- vantages to object-oriented technologies.

 When a message is sent to an object, the object musthave a method defined to respond to that message.

In an inheritance hierarchy, all subclasses inherit the

interfaces from their superclass. However, because each subclass is a separate entity,

each might require a separate response to the samemessage.

Page 28: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 28/36

Overriding  basically means replacing an implementationof a parent with one from a child.

public abstract class Shape{private double area;

public abstract double getArea();}

public class Circle extends Shape{double radius;

public Circle(double r) {

radius = r; }

public double getArea() {

area = 3.14*(radius*radius);

return (area);}}

Page 29: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 29/36

Constructor

 When a method name is the same as the class and noreturn type is provided, the method is a special method,called a constructor.

Consider a constructor as the entry point for the class, where the object is built; the constructor is a good place

to perform initializations and start-up tasks.

Page 30: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 30/36

Page 31: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 31/36

Terminologies Data Structure

Data Type

Stack

Queue

 Array

Page 32: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 32/36

Composition It is natural to think of objects as containing other

objects.

Eg. : A computer contains video cards, keyboards, anddrives.

Objects are often built, or composed, from otherobjects, This is composition.

 Abstraction

 Just as with inheritance, composition provides a

mechanism for building objects. There are only two ways to build classes from other

classes: inheritance and composition.

Page 33: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 33/36

 As Inheritance allows one class to inherit from another

class. We can thus abstract out attributes and behaviors for

common classes.

Consider the relationship between a car and an engine.

Has-a Relationships

 we use the term has-a to describe compositionrelationships.

E.g.. : A car has-a(n) engine.

Page 34: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 34/36

Lets Recall Important out of all Encapsulation—Encapsulating the data and behavior

into a single object is of primary importance in OOdevelopment. A single object contains both its dataand behaviors and can hide what it wants from otherobjects.

Inheritance— A class can inherit from another classand take advantage of the at-tributes and methodsdefined by the superclass.

Polymorphism—Polymorphism means that similarobjects can respond to the same message in different ways.

Composition—Composition means that an object is

built from other objects.

Page 35: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 35/36

Doubts / Questions?????

Page 36: Chap1_Introduction to OOP

8/12/2019 Chap1_Introduction to OOP

http://slidepdf.com/reader/full/chap1introduction-to-oop 36/36

END