More Inheritance and LSP CS340100, NTHU Yoshi. More about Inheritance Reuse? – Q1: 你有沒有程式 ” 砍掉重練 ” 的經驗 ? – Q2: 你有沒有 ” 再造輪子 ” 的經驗 ? class Rectangle – Firstly,

Slides:



Advertisements
Similar presentations
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
5/17/2015 OO Design: Liskov Substitution Principle 1.
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
Midterm Exam Discussion. Lab1 Part package cs homework1; abstract class Shape { public abstract double getArea(); public boolean equals(Object.
Course Introduction CLASS II. 1-2 Previous studies What is constructor? How to use this()? –Where I can call this()? –Can I call this() twice in one constructor?
Chapter 8, Object Design: Reuse and Patterns Qutaibah Malluhi Software Engineering Qatar University Based on slides by Bernd Bruegge & Allen H. Dutoit.
Typing Issues and LSP Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Readability (Java vs. Pearl) Catching errors.
1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,
אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 1 פרק 4 LSP.
Typing Issues and LSP David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Reliability Catching errors early Readability.
CS 142 Lecture Notes: JavascriptSlide 1 Simple Javascript Example sum = 0; for (i = 1; i < 10; i++) { sum += i*i; }
CSc 335: Three More OO Design Principles
Design II Today: Design Principles…can we define them intro.12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger.
UML Basics & Access Modifier
1 OO Design Principles Project Group eXtreme Programming Md. Abul Bashar 07/09/2004.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
3-4 Lesson 3-4 Example 1 Use the formula A = ℓ w to solve for ℓ, length. The area of the rectangle is 72 square yards. Its width is 9 yards. What is the.
More Design. Next Tuesday First draft of architectural design –use cases –class diagrams for major classes with responsibilities –sequence diagrams for.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Design Principles iwongu at gmail dot com.
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
Software Design Principles
1 Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Law of Demeter CSC 335: Object-Oriented Programming and Design.
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
CSE 331 Software Design & Implementation Hal Perkins Winter 2012 Design Patterns Part 2 (Slides by Mike Ernst and David Notkin) 1.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
Test Code Patterns How to design your test code. 2 Testing and Inheritance Should you retest inherited methods? Can you reuse superclass tests for inherited.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
SOLID PHP & Code Smell Wrap-Up
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
SW Design Principle YoungSu, Son. Microsoft MVP EvaCast Study Leader Devpia Architecture Sysop.
Procedural and Object-Oriented Programming
More Design Heuristics
Software Design Principles
Inheritance ITI1121 Nour El Kadri.
Inheritance class TwoDShape { private double width;
Advanced Program Design with C++
CSE 331 Software Design & Implementation
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
EECE 310: Software Engineering
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CS 200 Creating Classes Jim Williams, PhD.
Introduction to Classes
Software Design Principles
Subtype Polymorphism, Subtyping vs
Chapter 11 Inheritance and Polymorphism
CS 200 Creating Classes Jim Williams, PhD.
CS 200 More Classes Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
Software Development An overview of careers in Business Application Development Sources: Dice.com, money.CNN.com, InfoWorld.com, money.USNews.com,
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 8 Object Oriented Programming (OOP)
Liskov Substitution Principle (LSP)
Presentation transcript:

More Inheritance and LSP CS340100, NTHU Yoshi

More about Inheritance Reuse? – Q1: 你有沒有程式 ” 砍掉重練 ” 的經驗 ? – Q2: 你有沒有 ” 再造輪子 ” 的經驗 ? class Rectangle – Firstly, we only have this design, and the system was developed class Square – Then, we have the requirement of Square, and we wrote a class Square – Square “IS A” kind of rectangle 1-2

Class Rectangle class Rectangle { double width; double height; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } } 1-3

Class Square class Square extends Rectangle { public void setHeight(double height) { super.setHeight(height); super.setWidth(height); } public void setWidth(double width) { super.setHeight(width); super.setWidth(width); } } 1-4

For Instance… void g(Rectangle r) { r.setWidth(5); r.setHeight(4); if (r.getWidth() * r.getHeight() != 20) { throw new RuntimeException(); } } //some other place Rectangle square = new Square(); g(square); 1-5

Liskov Substitution Principle (LSP) Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it Very difficult problem!! – Further study: OOAD – Software design principals Please google it if you are interested in it One of the principals – Prefer Composition over inheritance – Why? Can you give an example? 1-6

Delegation 1-7 Inheritance Aggregation