Software Engineering Fall 2005

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Abstract Class and Interface
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Programming in Java CSCI-2220 Object Oriented Programming.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Coming up: Inheritance
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Object-Oriented Design
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Week 4 Object-Oriented Programming (1): Inheritance
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Object Oriented Programming
Lecture 23 Polymorphism Richard Gesick.
Overloading and Constructors
Object-Oriented Programming
Object-Oriented Programming: Polymorphism
Java Programming Language
Advanced Java Programming
More About Inheritance & Interfaces
Java Inheritance.
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Chapter 14 Abstract Classes and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
C++ Object Oriented 1.
Presentation transcript:

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

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

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

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

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

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); } }

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()

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; } }

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); } }

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) ?

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

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 ?

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

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

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 {…

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

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)

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()

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”

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);

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

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) {…}

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

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