Designing Data Types. 2 Object Oriented Programming Procedural programming. [verb-oriented] n Tell the computer to do this. n Tell the computer to do.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance and Polymorphism CS351 – Programming Paradigms.
3.3 Designing Data Types Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · September.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Salman Marvasti Sharif University of Technology Winter 2015.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CS102 – OOP_2 Inheritance, Polymorphism, Interfaces & Abstract classes. David Davenport.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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,
Object-Oriented Programming
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
Software Engineering Fall 2005
Object Oriented Programming in Java
12 Data abstraction Packages and encapsulation
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Object Oriented Analysis and Design
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Classes in C++ C++ originally called "C with classes":
Polymorphism and access control
Week 6 Object-Oriented Programming (2): Polymorphism
Java Inheritance.
CISC/CMPE320 - Prof. McLeod
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Building Java Programs
Object-Oriented Programming: Classes, Inheritance, and Interfaces
Review of Previous Lesson
Object-Oriented PHP (1)
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Designing Data Types

2 Object Oriented Programming Procedural programming. [verb-oriented] n Tell the computer to do this. n Tell the computer to do that. Alan Kay's philosophy. Software is a simulation of the real world. n We know (approximately) how the real world works. n Design software to model the real world. Objected oriented programming (OOP). [noun-oriented] n Programming paradigm based on data types. n Identify things that are part of the problem domain or solution. n Things in the world know things: instance variables. n Things in the world do things: methods.

3 Encapsulation

4 Data type. Set of values and operations on those values. Ex. int, String, Complex, Vector, Document, GuitarString, Tour, … Encapsulated data type. Hide internal representation of data type. Separate implementation from design specification. n Class provides data representation and code for operations. n Client uses data type as black box. n API specifies contract between client and class.

5 Intuition ClientAPI - volume - change channel - adjust picture - decode NTSC signal Implementation - cathode ray tube - electron gun - Sony Wega 36XBR pounds Implementation and client need to agree on API ahead of time. client needs to know how to use API implementation needs to know what API to implement

6 Intuition API - volume - change channel - adjust picture - decode NTSC signal Implementation - gas plasma monitor - Pioneer PDP-502MX - wall mountable - 4 inches deep Can substitute better implementation without changing the client. Client client needs to know how to use API implementation needs to know what API to implement

7 Counter Data Type Counter. Data type to count electronic votes. Legal Java client. The candidate receives -16,022 votes in Boston. public class Counter { public int count; public final String name; public Counter(String id) { name = id; } public void increment() { count++; } public int value() { return count; } } Counter c = new Counter(”Boston"); c.count = ;

8 Counter. Encapsulated data type to count electronic votes. Does not compile. Benefit. Can guarantee that each data type value remains in a consistent state. public class Counter { private int count; public final String name; public Counter(String id) { name = id; } public void increment() { count++; } public int value() { return count; } } Counter Data Type Counter c = new Counter(”Boston"); c.count = ;

9 Changing Internal Representation Encapsulation. Keep data representation hidden with private access modifier. Expose API to clients using public access modifier. Advantage. Can switch internal representation without changing client. Note. All our data types are already encapsulated! public class Complex { private final double re, im; public Complex(double re, double im) { … } public double abs() { … } public Complex plus(Complex b) { … } public Complex times(Complex b) { … } public String toString() { … } } e.g., to polar coordinates

10 Time Bombs Internal representation changes. n [Y2K] Two digit years: January 1, n [Y2038] 32-bit seconds since 1970: January 19, n [VIN numbers] We'll run out by Lesson. By exposing data representation to client, need to sift through millions of lines of code in client to update.

11 Encapsulated data types. n Don't touch data and do whatever you want. n Instead, ask object to manipulate its data. Lesson. Limiting scope makes programs easier to maintain and understand. "Ask, don't touch." Ask, Don't Touch

Immutability

13 Immutability Immutable data type. Object's value cannot change once constructed.

14 Immutability: Advantages and Disadvantages Immutable data type. Object's value cannot change once constructed. Advantages. n Avoid aliasing bugs. n Makes program easier to debug. n Limits scope of code that can change values. n Pass objects around without worrying about modification. Disadvantage. New object must be created for every value.

15 Final Access Modifier Final. Declaring an instance variable to be final means that you can assign it a value only once, in initializer or constructor. Advantages. n Helps enforce immutability. n Prevents accidental changes. n Makes program easier to debug. n Documents that the value cannot not change. public class Counter { private final String name; private final int maxCount; private int count;... } this value changes by invoking instance method these values don’t change once the object is constructed

Example: Spatial Vectors

17 Vector Data Type Set of values. Sequence of real numbers. [Cartesian coordinates] API. x = (0, 3, 4, 0), y = (0, -3, 1, -4) x + y = (0, 0, 5, -4) 3x = (0, 9, 12, 0) x  y = (0  0) + (3  -3) + (4  1) + (0  -4) = -5 | x | = ( ) 1/2 = 5 x = x / | x | = (0, 0.6, 0.8, 0)

18 Vector Data Type Applications Relevance. A quintessential mathematical abstraction. Applications. n Statistics. n Linear algebra. n Clustering and similarity search. n Force, velocity, acceleration, momentum, torque. n …

19 Vector Data Type: Implementation public class Vector { private int N; private double[] coords; public Vector(double[] a) { N = a.length; coords = new double[N]; for (int i = 0; i < N; i++) coords[i] = a[i]; } public double dot(Vector b) { double sum = 0.0; for (int i = 0; i < N; i++) sum += (coords[i] * b.coords[i]); return sum; } public Vector plus(Vector b) { double[] c = new double[N]; for (int i = 0; i < N; i++) c[i] = coords[i] + b.coords[i]; return new Vector(c); } constructor instance variables methods

20 This. The keyword this is a reference to the invoking object. Ex. When you invoke a.magnitude(), this is an alias for a. public Vector times(double t) { double[] c = new double[N]; for (int i = 0; i < N; i++) c[i] = t * coords[i]; return new Vector(c); } public double magnitude() { return Math.sqrt(this.dot(this)); } public Vector direction() { return this.times(1.0 / this.magnitude()); }... Vector Data Type: Implementation

Relations between Classes Relations between classes: Dependence: “use a”. Aggregation: “has a”. Inheritance: “is-a” 21 RushOrder Order Item Account 1 *

Inheritance 22 Rectangle Shape Equilateral CircleTriangle Base class (super class) Derived classes (subclasses) 1.Derived classes inherit all the public fields and public methods from base classes. 2.Derived classes CAN override the methods in base classes. 3.Derived classes CANNOT remove “properties” or “methods”. They can only add or modify. public class Triangle extends Shape {…} public class Rectangle extends Shape {…}

Abstract Base Class The Shape class is an “abstract” class It contains two “abstract” methods which do not have bodies. They have to be “defined” in the derived classes. 23 import java.awt.Color; public abstract class Shape { public Color color; public double lineWidth; public abstract double getArea(); public abstract void draw(); }

The Circle Class defines the draw() and getArea() methods. 24 import java.awt.Color; public class Circle extends Shape { double x, y; double r; public Circle(double x, double y, double r, double l, Color c) { this.x = x; this.y = y; this.r = r; color = c; lineWidth = l; } public double getArea() { return Math.PI * r * r; } public void draw() { StdDraw.setPenColor(color); StdDraw.setPenRadius(lineWidth); StdDraw.circle(x, y, r); }

Polymorphism and Dynamic Binding In Java object variables are polymorphic, they can indicate the objects of a class or its subclasses. When the methods are called, they are checked so that the correct method are dynamically bound to the object reference. We can use this property to batch process a large number of objects using the same interface. 25 Shape s1 = new Circle(0.5, 0.5, 0.1, 0.01, new Color(255,0,0)); Shape s2 = new Rectangle(0.5, 0.5, 0.2, 0.3, 0.01, new Color(0,255,0)); s1.draw(); s2.draw();

Interface Java interface is a special abstract class. It is usually used to define a common interface. 26 public interface Drawable { public void draw(); } import java.awt.Color; public class Circlex implements Drawable { double x, y; double r; double lineWidth; Color color; public Circlex(double x, double y, double r, double l, Color c) { this.x = x; this.y = y; this.r = r; color = c; lineWidth = l; } public void draw() { StdDraw.setPenColor(color); StdDraw.setPenRadius(lineWidth); StdDraw.circle(x, y, r); } The interface implementation