Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Engineering Fall 2005

Similar presentations


Presentation on theme: "Software Engineering Fall 2005"— Presentation transcript:

1 Software Engineering Fall 2005
Lec02 Basic building blocks for data and hierarchical abstraction in Java Software Engineering Fall 2005

2 Teaching Assistants 下面是部分TA的联系方式: 张艳 冯斌 如果大家在学习这门课的时候有疑问,可以和我们联系,我们会尽力帮大家解决!

3 Previously Decomposition and abstraction are techniques to construct large programs that are easy to understand, maintain and modify Abstraction = hide details Parameterization generalizes to wider applicability Specification separates behavior from implementation details Four kinds of abstraction: Procedural abstraction Data abstraction Iteration abstraction Type hierarchy

4 Building blocks for abstraction
Procedural abstraction -> method declaration Float sqrt (float coef) { // Requires: coef > 0 // Effect: Returns an approximation of square root of coef } Data abstraction -> class declaration Type hierarchy -> class declarations + inheritance

5 Abstract Classes Java provides a mechanism for defining "prescriptive" classes by placing the keyword abstract in front of a class - the abstract class cannot be instantiated, only its subclasses can - the abstract class can define abstract methods which subclasses must implement to be concrete (can be instantiated) - the abstract class can define methods and attributes which all subclasses inherit

6 Example: class Point public abstract class Point {
public abstract float x(); public abstract float y(); public float distance(Point other) { // Effect: Returns the Euclidian distance between two points float dx = other.x() – this.x(); float dy = other.y() – this.y(); return sqrt(dx*dx + dy*dy); } }

7 Class Point x() and y() are abstract only the method signature is specified -> a concrete subclass must provide an implementation distance(Point other) is concrete the implementation defines the notion of Euclidian distance, independent of the actual implementation of x() and y()

8 Subclass CartesianPoint
public class CartesianPoint extends Point { private float _x, _y; public CartesianPoint (float x, float y) { _x = x; _y = y; } public float x() { return _x; } public float y() { return _y; } }

9 Subclass PolarPoint public class PolarPoint extends Point {
private float _rho, _theta; public PolarPoint (float rho, float theta) { _rho = rho; _theta = theta; } public float x() { return _rho * cos(_theta); } public float y() { return _rho * sin(_theta); } }

10 Abstraction in Point x() y() distance() provide abstraction by specification: all Points have these methods distance() provides parameterization abstraction: implementation applies to all Points e.g. Point c = new CartesianPoint(2.5,-3); Point p = new PolarPoint(2,3.14); c.distance(p) ?

11 Abstract Classes tell the user which functionality is supported by concrete sub classes tell the implementor of sub classes which functionality should be realized Serve as a contract : defines the interface a user can count on Implement functionality which all sub classes have in common (concrete methods) Use abstract super class when subclasses only re-uses part of the implementation Use concrete super class when a subclass is a true extension: + fields + methods

12 Why extend a class ? Two reasons:
a) interface of sub class includes interface of super class -> subclass looks like super class from the outside -> used anywhere the super class is used b) implementation of sub class is similar to super class -> re-use implementation code -> inherit fields + method implementations What if you don’t re-use implementation ?

13 Interfaces Interfaces are like abstract classes, but
without any concrete methods (all methods are public and abstract) without any fields other than static final fields (constants) use keyword implements instead of extends A class can extend only one class, but can implement multiple interfaces

14 Point as Interface public interface Point {
public float x(); public float y(); public float distance(Point other); } public class CartesianPoint implements Point {... public class PolarPoint implements Point {… No longer any default implementation for distance(Point other), but now unrelated classes can provide Point functionality

15 Multiple Interfaces public interface WeightedObject {
public float weight(); } public interface ColoredObject { public float red(); public float green(); public float blue(); } public class CartesianPoint implements Point, WeightedObject, ColoredObject {… public class PolarPoint implements Point, WeightedObject, ColoredObject {…

16 Interfaces vs. Abstract Classes
Abstract classes define a hierarchical relationship: class B is a special type of class A B has all of A’s fields and methods (you are a special case of only one class) Interfaces define a form of behavior: Class B behaves as specified by interface A (you can behave like multiple interfaces) Multiple supers only allowed for interface construction, not for implementation

17 Why no multiple inheritance ?
diamond problem: if A inherits from B and C and B and C both inherit from D does an object of type A have one or two versions of the field defined in D ? method dispatch ambiguity: if A inherits from B and C and B and C both define method foo() which method is executed on A.foo() ? Any proposed solution has its problems (huge objects in C++, obfuscated control flow)

18 Method Overriding New definition for method in a subclass: same parameter numbers, type and order Appropriate method is called by dynamic binding: determine run time class of receiver object -> can not be determined at compile time e.g.: A and B inherit from C C someObject; if (mouseClicked) someObject = new A(); else someObject = new B(); someObject.foo()

19 Method Overloading Methods with same name but different parameters different number different type different order Appropriate method is invoked based on the declared type of parameters -> this can be determined at compile time “syntactic sugar”

20 What is printed ? Public class A { foo (A a) {System.out.println(“1”);} foo (B b) {System.out.println(“2”);}}} Public class B extends A { foo (A a) {System.out.println(“3”);}} foo (B b) {System.out.println(“4”);}}} A aa = new A; A ab = new B; B bb = new B; aa.foo(aa); aa.foo(ab); aa.foo(bb); ab.foo(aa); ab.foo(ab); ab.foo(bb); bb.foo(aa); bb.foo(ab); bb.foo(bb);

21 Confusion A aa = new A; A ab = new B; B bb = new B;
aa.foo(aa); aa.foo(ab); aa.foo(bb); ab.foo(aa); ab.foo(ab); ab.foo(bb); bb.foo(aa); bb.foo(ab); bb.foo(bb); For parameters, care only about the declared type For receiver, care only about the instantiated type => to avoid confusion don’t mix overloading with overriding Except to define default parameters

22 Responsible overloading
class Document { public print () { this.print(defaultPrinter) } public print (Printer p); // print this document on Medium m } class pdfFile extends Document { public print (Printer p) {…} class psFile extends Document { public print (Printer p) {…}

23 Design Patterns By using these basic components, we can construct software architectures with re-usable components. Design patterns = collections of data abstractions programming tricks common usage of object-oriented constructs similar to architectural standards (“living room”) a common language for software architectures Iteration abstraction is a design pattern

24 Summary Java has a rich set of abstraction building blocks:
classes -> use single inheritance abstract classes concrete classes interfaces -> use multiple inheritance methods concrete methods abstract methods overriding -> new method definition for subclass overloading -> different method with same name design patterns are built from basic constructs


Download ppt "Software Engineering Fall 2005"

Similar presentations


Ads by Google