lecture 2
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/1.jpg)
Lecture 2
Object-oriented programming
![Page 2: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/2.jpg)
Definitions of OOP OOP is a programming paradigm,
which utilizes encapsulation, inheritance and polymorphism.
(From standard programming
languages textbook)
![Page 3: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/3.jpg)
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)
![Page 4: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/4.jpg)
Definitions of OOPOOP is a way of modeling computation which attempts to mimic the way we see the world around us.
![Page 5: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/5.jpg)
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
![Page 6: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/6.jpg)
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.
![Page 7: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/7.jpg)
Encapsulationpublic class Car { private String serialNumer; private String make; private double tankVolume; private double speed;}
![Page 8: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/8.jpg)
Encapsulationpublic 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;
}
![Page 9: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/9.jpg)
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)
![Page 10: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/10.jpg)
Inheritance Is-a relationship (is kind of)
Dog is a Mammal Professor is (usually) Human Running is an Exercise
![Page 11: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/11.jpg)
Inheritance is about organizing abstractions into hierarchies.
![Page 12: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/12.jpg)
Inheritance 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.
![Page 13: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/13.jpg)
Inheritance
Derived class inherits interface and implementation from the base class.
![Page 14: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/14.jpg)
Inheritance for extension 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 } }
![Page 15: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/15.jpg)
Inheritance for reuse 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 }}
![Page 16: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/16.jpg)
An interface is an expression of pure design, whereas class is a mix of design and implementation.
From Arnold & Gosling 'Java programming language'
![Page 17: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/17.jpg)
public interface IDatabase { public String getName(); public ITable getTable( String name ); public Iterator tables(); public Iterator tableNames();}
![Page 18: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/18.jpg)
public class RelationalDatabase implements IDatabase
public class FileDatabase implements IDatabase
![Page 19: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/19.jpg)
public void setDatabase( IDatabase db ) { if ( db instanceof RelationalDatabase ) { processRelationalDatabase(
(RelationalDatabase)db ); } else if ( db instanceof FilelDatabase ) { processFilelDatabase( (FilelDatabase)db ); } ...} DON’T DO THIS….
![Page 20: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/20.jpg)
class GraphicalTextEditor :
public TextEditor, Component;
![Page 21: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/21.jpg)
public class TextEditor {}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();}
![Page 22: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/22.jpg)
AggregationObjects of a class have a reference
(member field) one or more objects of another class
public class Car {…private Engine engine;
}
![Page 23: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/23.jpg)
Object of another class is passed as an argument into a method.
public class PnLCalculator { public static double computePnL(
ITrade[] tradeList ) { } private PnLCalculator() {}}
Uses
![Page 24: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/24.jpg)
Interface can extend another interface
public interface List extends Collection
![Page 25: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/25.jpg)
public interface IDynamicProperties { 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();}
![Page 26: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/26.jpg)
public class Entity 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; } }
![Page 27: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/27.jpg)
public class TreeNode { 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() ); } }}
![Page 28: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/28.jpg)
public interface Iterator { public boolean hasNext(); public Object next();
public void remove();}
![Page 29: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/29.jpg)
for ( Iterator i = getChildren(); i.hasNext(); ) { TreeNode child = (TreeNode)i.next(); newNode.addChild( child.deepCopy() );}
![Page 30: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/30.jpg)
public abstract class Entity
public String getName() { return name; }
public void setName( String name ) { this.name = name; } public abstract Object getUniqueId(); private String name;
}
![Page 31: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/31.jpg)
IFoo
AbstractFoo
DefaultFoo ABitDifferentFoo
VeryDifferentFoo
![Page 32: Lecture 2](https://reader035.vdocuments.net/reader035/viewer/2022070503/5681566a550346895dc41ce1/html5/thumbnails/32.jpg)
Please read• Chapters 2, 4, 5 ,6, 10, 11 in Java
programming language by Arnold & Gosling
• Chapters 1 and 2 in Design Patterns by Gamma, etc.