• No results found

lecture-1-ds-review-2013-2.pdf

N/A
N/A
Protected

Academic year: 2020

Share "lecture-1-ds-review-2013-2.pdf"

Copied!
27
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Object Oriented

• The main idea of OO is to organize a program in a way that mirrors the real world objects

(attributes and behaviors).

• Object oriented program: is a collection of interacting objects.

• Object: is a program construct that

– Contains data

– Performs certain actions ( methods ).

• Objects of the same class have

(3)

Classes

• Class: is a new data type (user defined

data type) that describes what the object is

and what it can do.

• Class: data type, plan, template, or a

(4)

References and Aliases

• Primitive types:

byte, short, int, long

float, double, char, boolean

• All other types are reference or class types

String greeting = "Howdy";

greeting is a reference variable

– Reference: is a variable that holds the memory address of a particular object.

(5)

Constructors

• Constructor : is A method that

– Allocates memory for the object – Initializes the data fields

• Properties

– Same name as the class

– No return type, not even void

– Any number of formal parameters

• Constructors can be:

- default constructors

(6)

Creating objects

• Objects created from classes by: - declaring a variable (reference) - using the constructor of the class - using the New statement

• In this case the created object is an instance of a class.

• Array of objects

• Garbage collection • Packages

(7)

Main concepts and features of

object oriented programming

• Encapsulation

• Abstraction

• Inheritance

(8)

Encapsulation

• Hides the detail of the inner workings of the class

– The implementation is hidden – Often called "information hiding"

• Part of the class is visible

– The necessary controls for the class are left visible – The class interface is made visible

(9)

Abstraction

• A process that has the designer ask what

instead of why

– What is it you want to do

with

the data – What will be done

to

the data

• The designer does not consider how the

class's methods will accomplish their goals

• The client interface is the what

(10)

Abstraction

(11)

Inheritance

• Inheritance: is a process of deriving new class from already existing class.

• A general or base class is first defined Then a more specialized class is defined by …

– Adding to details of the base class (data/methods) – Revising details of the more general class (overriding) • Advantages

– Saves work

– Common properties and behaviors are define only once for all classes involved

• In a derived class

– The constructor must call the base class constructor

• Can use the reserved word super as a name for the constructor of the base class

– When super is used, it must be the first action in the derived constructor definition

(12)

Inheritance

(13)

Object Types of a Derived Class

• Given :

– Class

CollegeStudent

,

– Derived from class

Student

• Then a CollegeStudent

object is also a

Student

object

• In general …

(14)

Polymorphism

• When one method name in an instruction can cause different actions – Happens according to the kinds of objects that invoke the

methods

– When an overridden method is used The action is for the method defined in the class whose constructor created the object

– An object, not its name, determines its behavior. • Example

The object still remembers it is of type UndergradStudent

UndergradStudent ug = new UndergradStudent(. . .); Student s = ug; // s and ug are aliases

(15)

Interface

• A program component that contains

– Public constants

– Signatures for public methods – Comments that describe them

• Begins like a class definition

– Use the word interface instead of class

public interface someClass {

(16)

Java Interface Example

public interface NameInterface

{ public void setName(String firstName, String lastName);

public String getName();

public void setFirst(String firstName);

public String getFirst();

public void setLast(String lastName);

public String getLast();

public void giveLastNameTo(NameInterface child);

(17)

Software specification

• Software specification : is a detailed description of the operations (functions , methods ), inputs, processing, outputs, and special requirements of a software product.

• Specifying Methods:

– Specify what each method does – Precondition

• Defines responsibility of client code (client/method responsibility)

– Postcondition

• Specifies what will happen if the preconditions are met

– Assertions can be written as comments to identify design logic

(18)

Example of methods specifying

1. Client responsibility:

/** task: computes the square root of a number *@ param x a real number>=0

*@ return the square root of x */

2. Method responsibility:

/** task: computes the square root of a number *@ param x a real number

(19)

Implementing an Interface

• A class that implements an interface must state so at start of definition

public class myClass implements someInterface

• The class must implement every method declared in the interface

• Multiple classes can implement the same interface

• A class can implement more than one interface • An interface can be used as a data type

(20)
(21)

Type Casts Within an Interface

Implementation

• In any class that implements an interface

– A parameter may be of type Object

– A type cast would probably be needed within that method public class Pet implements Comparable

{ private String name;

private int age; // in years

private double weight; // in pounds

/** Task: Compares the weight of two pets. */

public int compareTo (Object other) { Pet otherPet = (Pet)other;

return weight - otherPet.weight; } // end compareTo

< Other methods are here. >

(22)

Composition

• When a class has a data field that is an instance of another class

• Example – an object of type Student.

• A Student object composed of other objects

(23)

Inner classes

• An inner class is an example of composition

• An inner class, or nested class, is a class defined within the scope of another class. • Advantages:

- can make programs simple

- an inner class can reference the data and methods of outer class without passing outer class reference

• Example:

public class outerclass{ private int data;

public void m(){ //do some thing

innerclass obj1= new innerclass(); }// end of m()

class innerclass{ public void mi(){

data ++; m();

(24)

Aggregation and composition

• Aggregation and composition represent two kinds of containment relationships between instances of the corresponding classes

• Aggregation

– Contained object is part of container, but can exist independently – Ownership of contained object is a policy decision

– Contained object may be part of other aggregations

– Example: In Java, create an object and place it in a container, but maintain an external reference to object:

• ArrayList aList = new ArrayList();

(25)

Aggregation and composition

Composition

– Contained object does not exist independently of its container

– Contained object is owned by container

– Contained object may be part of only one composition (only one owner)

– Example: In Java, create an anonymous instance and place it directly in a container, with no external

reference to the object:

(26)

Aggregation and composition

• Example: Create an object in a class constructor: • /**

• * Sample of composition. • */

• public class MyIntegerClass {

• private Integer value; // Value of instance. • /**

• * Constructor initializing the instance value.

• *

• * @param value value assigned to instance

• */

• public MyIntegerClass( int value ) { • this.value = new Integer( value );

• }

(27)

References

Related documents

Operational Flexibility: Simplicity of Protection Arrangements Ability to limit Short Circuit Levels (Ease of Sectionalizing) Switching operation to take out the breaker

In addition, the first-year turnover rate (the number of employees who leave before being at the institution even just one year) is even higher: 28.3% on average iii.. This means

Page2/32 SPACE FOR ROUGH WORK / Sample Test Paper-1..   This section contains

The increase in the rate of return from the “safe” crop investment reduces the actuarially fair crop insurance effect on “risky” crop investment if the farmers have CARA preference

For each filter examined, a picture is presented showing the location of the filter on the NorCal 40A printed circuit board, followed by a schematic diagram of the selected

Medicine will be the first sphere to start the final phase of the Cybernetic Revolution, but, later on, self-regulating systems development will cover the most diverse areas

Among both those visitors who had visited a museum in San Antonio in the past 12 months or planned to do so in the next six, seeing friends or relatives as well as going to

The present work evaluates the nutritional and feed value of fermented sweet potato meal (ProEn-K TM ) to replace soybean meal in the diet of juvenileP.