“OOP” Principles and Philosophy Classes and Objects Inheritance Message Passing Encapsulation Overloading Polymorphism
Classes and Objects class ComplexShape class SimpleShape ACTIVITY 1
Inheritance public class ComplexShape extends SimpleShape { } ACTIVITY 1
“ Calling Methods” = “Message Passing” public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setColor(COLOR_BACKGROUND); g2.fillRect(0,0,width,height); for(i=0; i < shapesArray.length;i++) { shapesArray[i].draw(g); } public void draw(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setPaint(color); g2.setStroke(new BasicStroke(2)); if(type == 1) g2.draw(myShape); if(type == 2) g2.fill(myShape); } DisplayCanvas object SimpleShape object “Hey SimpleShape object – draw yourself!”
Encapsulation Iterator it = shapeList.iterator(); while(it.hasNext()) { SimpleShape shape = (SimpleShape)it.next(); shapeX = shape.getX(); shapeY = shape.getY();... } Private double x,y; public double getX() { return x; } DisplayCanvas object SimpleShape object shapeX = shape.x; Public double x; ACTIVITY 2
Overloading of methods public voiddraw(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setStroke(new BasicStroke(2)); if(type == 1) g2.draw(myShape); if(type == 2) g2.fill(myShape); } public voiddraw(Graphics g) { Graphics2D g2 = (Graphics2D)g; super.draw(g); g2.setStroke(new BasicStroke(10)); g2.setPaint(color2); g2.draw(newBit); } ACTIVITY 1
Polymorphism ACTIVITY 4 public Ball(double x, double y, DrawCanvas canvas) { super(x,y,canvas); radius = 10; width = (2*radius); height = (2*radius); super.setWidth(width); … } public Ball(double x, double y, String imageName, DrawCanvas canvas) { super(x,y,canvas); image = loadImage(imageName); } Class Ball Constructor 1 Constructor 2 Ball ball1 = new Ball(10,20,canvas); Ball ball2 = new Ball(12,32,”Bush.jpg”,canvas)
Modelling (Carvaggio In Study)
UML Software Tools Microsoft Visio DirectUML (Module Web-page) Fujaba Jude …
Dependency ACTIVITY 1 Task 1 UML Objects myShape = new SimpleShape(200,200,100,100,0.6); Class Object
Dependency UML Object Diagram ACTIVITY 1 Task 1
UML Class Diagram ACTIVITY 1 Task 1 Inheritance
UML Sequence Diagram Time