Two 2x2 (block) matrices can be multiplied C==AB

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
OOP: Inheritance By: Lamiaa Said.
CS 211 Inheritance AAA.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Understanding Inheritance CompSci 230 S Software Design and Construction.
Inheritance using Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance.
Sections Inheritance and Abstract Classes
Advanced Java Topics Chapter 9
Inheritance and Polymorphism
Advanced C++ Topics Chapter 8.
Figure 8.1 Inheritance: Relationships among timepieces.
Extending Classes.
Ch. 4 Data Abstraction Modular programming Procedural abstraction
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

Example using Divide&Conquer Approach: More efficient matrix multiplication Two 2x2 (block) matrices can be multiplied C==AB [c11 c12; c21 c22]==[a11 a12; a21 a22][b11 b12; b21 b22] with 7 multiplications and 18 additions: Q1 = (a11+a22)(b11+b22) Q2 = (a21+a22)b11 Q3 = a11(b12-b22) Q4 = a22(-b11+b21) Q5 = (a11+a12)b22 Q6 = (-a11+a21)(b11+b12) Q7 = (a12-a22)(b21+b22). c11 = Q1+Q4-Q5+Q7 c21 = Q2+Q4 c12 = Q3+Q5 c22 = Q1+Q3-Q2+Q6 (Strassen 1969, Press et al. 1989). Which results in the recursive algorithm with time cost T(n)=7.T(n/2)+O(n2) Solving recurrence gives: T(n)=O(nlog27)=O(n2.81). log_2(7)=2.807

Inheritance Reusing the code CMPT 225 Inheritance Reusing the code

Inheritance Inheritance is a relationship between classes whereby one class can derive the behaviour and structure of another class Superclass or base class A class from which another class is derived Subclass, derived class, or descendant class A class that inherits the members of another class Benefits of inheritance to capture hierarchies that exist in a problem domain enables the reuse of existing classes which reduces the effort necessary to add features to an existing object reduces duplicated code cheaper to maintain (changes more localized) to specialize an existing class e.g. subclassing Swing components is encouraged in Java

Inheritance Fundamentals A subclass inherits all members (private and public) from the superclass, but private members cannot be accessed directly! Methods of a subclass can call the superclass’s public methods (and use public data members) Clients of a subclass can invoke the superclass’s public methods

Inheritance Fundamentals Additional data members and methods can be defined in subclasses Superclass methods can be overwritten/overridden in the subclass That is, a subclass may include its own definition of a method that exists in the superclass (they must have same declaration, not only the same name!) An overridden method Instances of the subclass will use the new method Instances of the superclass will use the original method

Example The subclass Ball inherits members of the superclass Sphere and overrides and adds methods

Example The subclass Ball inherits members of the superclass Sphere and overrides and adds methods

Example (Java) public class Sphere { private double theRadius; public Sphere() { setRadius(1.0); } // end default constructor public Sphere(double initialRadius) { setRadius(initialRadius); } // end constructor public boolean equals(Object rhs) { return ((rhs instanceof Sphere) && (theRadius == ((Sphere)rhs).theRadius)); } // end equals public void setRadius(double newRadius) { if (newRadius >= 0.0) { theRadius = newRadius; } // end if } // end setRadius public double radius() { return theRadius; } // end radius

Example (Java) public double diameter() { return 2.0 * theRadius; } // end diameter public double circumference() { return Math.PI * diameter(); } // end circumference public double area() { return 4.0 * Math.PI * theRadius * theRadius; } // end area public double volume() { return (4.0*Math.PI * Math.pow(theRadius, 3.0)) / 3.0; } // end volume public void displayStatistics() { System.out.println("\nRadius = " + radius() + "\nDiameter = " + diameter() + "\nCircumference = " + circumference() + "\nArea = " + area() + "\nVolume = " + volume()); } // end displayStatistics } // end Sphere

Example (Java) specify inheritance calls default constructor public class Ball extends Sphere { private String theName; // the ball's name // constructors: public Ball() { // Creates a ball with radius 1.0 and // name "unknown". setName("unknown"); } // end default constructor public Ball(double initialRadius, String initialName) { // Creates a ball with radius initialRadius and // name initialName. super(initialRadius); setName(initialName); } // end constructor // additional or revised operations: public boolean equals(Object rhs) { return ((rhs instanceof Ball) && (radius() == ((Ball)rhs).radius()) && (theName.compareTo(((Ball)rhs).theName)==0) ); } // end equals calls default constructor of Sphere first explicitely calls constructor Sphere(initialRadiud)

Example (Java) override method from the superclass public String name() { // Determines the name of a ball. return theName; } // end name public void setName(String newName) { // Sets (alters) the name of an existing ball. theName = newName; } // end setName public void resetBall(double newRadius, String newName) { // Sets (alters) the radius and name of an existing // ball to newRadius and newName, respectively. setRadius(newRadius); setName(newName); } // end resetBall public void displayStatistics() { // Displays the statistics of a ball. System.out.print("\nStatistics for a "+ name()); super.displayStatistics(); } // end displayStatistics } // end Ball override method from the superclass calls displayStatistics() from superclass Sphere

Example C++ specify inheritance calls default constructor class Ball : public Sphere { private: string theName; // the ball's name public: // constructors: Ball() { // Creates a ball with radius 1.0 and // name "unknown". setName("unknown"); } // end default constructor Ball(double initialRadius, string initialName) : Sphere(initialRadius) { // Creates a ball with radius initialRadius and // name initialName. setName(initialName); } // end constructor // etc. virtual void displayStatistics() { // Displays the statistics of a ball. cout <<"Statistics for a "<<name()<<endl; Sphere::displayStatistics(); } // end displayStatistics }; // end Ball calls default constructor of Sphere first explicitely calls constructor Sphere(initialRadiud) override method from the superclass calls displayStatistics() from superclass Sphere

Overriding Assume that mySphere is an instance of class Sphere and myBall is an instance of class Ball. What happens when we call displayStatistics() on othem? An object invokes the correct version of a method!

Java Access Modifiers Access to public, protected, package access, and private members of a class by a client and a subclass

Java Access Modifiers (complete list) Membership categories of a class Public members can be used by anyone Members declared without an access modifier (the default) are available to Methods of the class Methods of other classes in the same package Private members can be used only by methods of the class Protected members can be used only by Methods of the subclass

C++ Access Modifiers (complete list) Note: in C++ sections are used. They are specified by private: public: protected: (instead of prefixing each declaration). Membership categories of a class Public members can be used by anyone Private members can be used only by methods of the class Protected members can be used only by Methods of the class Methods of the subclass Note: the default section in class in C++ is private:

Is-a and Has-a Relationships When designing new classes from existing classes it is important to identify the relationship between the classes Two basic kinds of relationships Is-a relationship e.g. a graduate student is a student Has-a relationship e.g. a ball has a color

Is-a Relationship Inheritance should imply an is-a relationship between the superclass and the subclass Example: If the class Ball is derived from the class Sphere A ball is a sphere A ball “is a” sphere