Lecture 2
Download
1 / 32

Lecture 2 - PowerPoint PPT Presentation


  • 89 Views
  • Uploaded on

Lecture 2. Object-oriented programming. Definitions of OOP. OOP is a programming paradigm, which utilizes encapsulation, inheritance and polymorphism. (From standard programming languages textbook). Definitions of OOP.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture 2' - meadow


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Lecture 2

Lecture 2

Object-oriented programming


Definitions of oop
Definitions of OOP

OOP is a programming paradigm, which utilizes encapsulation, inheritance and polymorphism.

(From standard programming languages textbook)


Definitions of oop1
Definitions of OOP

OOP is a method of implementation in which programs are organized as cooperative collections of objects, each of which, represents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationship.

(Grady Booch,

Object-oriented analysis and design)


Definitions of oop2
Definitions of OOP

OOP is a way of modeling computation which attempts to mimic the way we see the world around us.


Grady booch on abstraction
Grady Booch on Abstraction

is one of the fundamental ways that we as humans cope with complexity

arises from recognition of similarities between certain objects, situations or processes in the real world

facilitates principle of least astonishment


Grady booch on abstraction1
Grady Booch on Abstraction

denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provides crisply defined conceptual boundaries, relative to the perspective of the viewer

captures the entire behavior of some object, no more, no less, and offers no surprises or side effects that go beyond the scope of the abstraction.


Encapsulation
Encapsulation

public class Car {

private String serialNumer;

private String make;

private double tankVolume;

private double speed;

}


Encapsulation1
Encapsulation

public class Car {

// Public section

public String getSerialNumber() { return serialNumber; }

public String getMake() { return make; }

public double getTankVolume() { return tankVolume; }

public double getSpeed() { return speed; }

// Private section

private String serialNumer;

private String make;

private double tankVolume;

private double speed;

}


Encapsulation2
Encapsulation

is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate contractual interface of an abstraction and its implementation.

(from Grady Booch’s Object-oriented analysis and design)


Inheritance
Inheritance

Is-a relationship (is kind of)

Dog is a Mammal

Professor is (usually) Human

Running is an Exercise



Inheritance1
Inheritance hierarchies.

At the top of inheritance hierarchy is a base class.

Typically, a base class would be abstract, meaning it would correspond to a concept, a not to a physical entity. This class would explicitly prohibit you to create instances of itself.

public abstract class Engine {}

Implies that

Engine myEngine = new Engine();

would not compile.


Inheritance2
Inheritance hierarchies.

Derived class inherits interface and implementation from the base class.


Inheritance for extension
Inheritance for extension hierarchies.

Is when derived class adds functionality to the base class

public class Point { public class ColorPoint

extends Point {

public int getX(); public Color getColor();

public int getY(); }

}

Another example

public class Derived extends Base {

public void build() {

super.build();

// do some more

}

}


Inheritance for reuse
Inheritance for reuse hierarchies.

Is when derived class doesn’t add functionality to most methods of the base class

Sometimes, the designer of a base class may intentionally specify a method as final.

public class Base {

public final void getName() {

return sName;

}

}

public class Derived extends Base {

public void getName() { // WOULD NOT COMPILE

}

}


An interface is an expression of pure design, whereas class is a mix of design and implementation.

From Arnold & Gosling 'Java programming language'


public interface IDatabase { is a mix of design and implementation.

public String getName();

public ITable getTable( String name );

public Iterator tables();

public Iterator tableNames();

}


public class RelationalDatabase is a mix of design and implementation.

implements IDatabase

public class FileDatabase

implements IDatabase


public void setDatabase( IDatabase db ) { is a mix of design and implementation.

if ( db instanceof RelationalDatabase ) {

processRelationalDatabase( (RelationalDatabase)db );

} else if ( db instanceof FilelDatabase ) {

processFilelDatabase( (FilelDatabase)db );

}

...

} DON’T DO THIS….


class GraphicalTextEditor : is a mix of design and implementation.

public TextEditor, Component;


public class TextEditor {} is a mix of design and implementation.

public class Component {}

public class GraphicalTextEditor extends TextEditor {

public Component getComponent() {

return c;

}

// Inner class GraphicalTextEditorComponent which takes care

// of the graphical aspect of this text editor.

private class GraphicalTextEditorComponent

extends Component {

}

private Component c =

new GraphicalTextEditorComponent();

}


Aggregation
Aggregation is a mix of design and implementation.

Objects of a class have a reference (member field) one or more objects of another class

public class Car {

private Engine engine;

}


Uses is a mix of design and implementation.

Object of another class is passed as an argument into a method.

public class PnLCalculator {

public static double computePnL(

ITrade[] tradeList ) {

}

private PnLCalculator() {}

}


Interface can extend another interface is a mix of design and implementation.

public interface List extends Collection


public interface IDynamicProperties { is a mix of design and implementation.

public void addProperty( String name, Object value );

public Object getProperty( String name );

public Object removeProperty( String name );

public Iterator properties();

public Iterator propertyNames();

}

public interface Clonable {

public Object clone();

}


public class Entity is a mix of design and implementation.

implements IDynamicProperties, Clonable {

..

// Implementation of Dynamic properties interface

public void addProperty( String name,

Object value ) {

htProperties.put( name, value );

}

...

// implementation of Clonable interface {

public Object clone() {

Entity newEntity = new Entity();

// copy all fields

return newEntity;

}

}


public class TreeNode { is a mix of design and implementation.

public Object deepCopy() {

TreeNode newNode = new TreeNode();

// copy all the fields

// copy children

for ( Iterator i = getChildren();i.hasNext(); ) {

TreeNode child = (TreeNode)i.next();

newNode.addChild( child.deepCopy() );

}

}

}


public interface Iterator { is a mix of design and implementation.

public boolean hasNext();

public Object next();

public void remove();

}


for ( Iterator i = getChildren(); i.hasNext(); ) { is a mix of design and implementation.

TreeNode child = (TreeNode)i.next();

newNode.addChild( child.deepCopy() );

}


public abstract class Entity is a mix of design and implementation.

public String getName() { return name; }

public void setName( String name ) {

this.name = name;

}

public abstract Object getUniqueId();

private String name;

}


IFoo is a mix of design and implementation.

AbstractFoo

VeryDifferentFoo

ABitDifferentFoo

DefaultFoo


Please read
Please read is a mix of design and implementation.

  • Chapters 2, 4, 5 ,6, 10, 11 in Java programming language by Arnold & Gosling

  • Chapters 1 and 2 in Design Patterns by Gamma, etc.


ad