Introduction to Object-Oriented Programming

Similar documents
Programming in the large

Introduction to Object-Oriented Programming

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle

Lecture: Modular Design

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

11/4/15. Review. Objec&ves. Refactoring for Readability. Review. Liskov Subs&tu&on Principle (LSP) LISKOV SUBSTITUTION PRINCIPLE

Object Oriented Software Design - I

Test Code Patterns. How to design your test code

Testing and Inheritance. Test Code Patterns. Inheritance-related bugs. Inheritance. Testing of Inheritance. Inheritance-related bugs

Chapter 6: Inheritance

Design Principles: Part 2

Objec&ves. Review. Liskov Subs&tu&on Principle Refactoring for Extensibility. What are reasons that we refactor our code?

Design Principles: Part 2

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

Principles of Object-Oriented Design

Chapter 7. Inheritance

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Inheritance and object compatibility

Object-oriented design principles

Liskov Substitution Principle

Software Engineering Design & Construction

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

CSC207 Week 3. Larry Zhang

Chapter 10 Classes Continued. Fundamentals of Java

S.O.L.I.D: Software Engineering Principles

CS-202 Introduction to Object Oriented Programming

CSC Advanced Object Oriented Programming, Spring Specification

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

Assertions, pre/postconditions

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

6.170 Recitation #5: Subtypes and Inheritance

9/10/2018 Programming Data Structures Inheritance

Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Advanced JML Erik Poll Radboud University Nijmegen

INHERITANCE. Spring 2019

Object Oriented Issues in VDM++

Type Hierarchy. Lecture 6: OOP, autumn 2003

Contracts. Dr. C. Constantinides. June 5, Department of Computer Science and Software Engineering Concordia University Montreal, Canada 1/71

CS111: PROGRAMMING LANGUAGE II

Practice for Chapter 11

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Introduction to Object-Oriented Programming

Programming II (CS300)

Principles of OO Design

Subtypes and Subclasses

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Conformance. Object-Oriented Programming Spring 2015

Comp 249 Programming Methodology

Inheritance, and Polymorphism.

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS 251 Intermediate Programming Inheritance

Object-Oriented Programming for High-Integrity Systems: Pitfalls and How to Avoid Them

CS Programming I: Inheritance

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

The Liskov Substitution Principle

Programming II (CS300)

Java Object Oriented Design. CSC207 Fall 2014

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Specification tips and pitfalls

Introduction to Object-Oriented Programming

Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Introduction to Object-Oriented Programming

Lecture 11 Subtypes and Subclasses

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Use the scantron sheet to enter the answer to questions (pages 1-6)

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object-Oriented Programming

Classes and Inheritance Extending Classes, Chapter 5.2

CMSC 132: Object-Oriented Programming II

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Inheritance, Polymorphism, and Interfaces

C++ Inheritance and Encapsulation

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

CSE 331 Software Design and Implementation. Lecture 12 Subtypes and Subclasses

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Object-oriented Programming. Object-oriented Programming

Defining Classes and Methods

CSE 331 Software Design & Implementation

IS-A / HAS-A. I IS-A Teacher and I HAS-A Laptop

Inheritance. Transitivity

CPSC 410? Advanced Software Engineering Mid-term Examination (Term I ) SOLUTION Instructor: Gail Murphy

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Object-Oriented Design

OO Design Principles

CS 215 Software Design Sample midterm solutions

Chapter 14 Abstract Classes and Interfaces

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Session 04 - Object-Oriented Programming 1 Self-Assessment

Advances in Programming Languages

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Transcription:

Introduction to Object-Oriented Programming Inheritance, Part 2 of 2 Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 1 / 14

Access Modifiers Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N Every class has an access level (for now all of our classes are public). Every member has an access level. The defulat access level, no mofifier, is also called package private. CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 2 / 14

Explicit Constructor Invocation with this What if we wanted to have default default values for hourly wages and monthly hours? We can provide an alternate constructor that delegates to our main constructor with this HourlyEmployee3.java: public final class HourlyEmployee3 extends Employee3 { /** * Constructs an HourlyEmployee with hourly wage of 20 and * monthly hours of 160. */ public HourlyEmployee3(String aname, Date ahiredate) { this(aname, ahiredate, 20.00, 160.0); public HourlyEmployee3(String aname, Date ahiredate, double anhourlywage, double amonthlyhours) { super(aname, ahiredate); disallowzeroesandnegatives(anhourlywage, amonthlyhours); hourlywage = anhourlywage; monthlyhours = amonthlyhours; //... CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 3 / 14

this and super If present, an explicit constructor call must be the first statement in the constructor. Can t have both a super and this call in a constructor. A constructor with a this call must call, either directly or indirectly, a constructor with a super call (implicit or explicit). public final class HourlyEmployee3 extends Employee3 { public HourlyEmployee3(String aname, Date ahiredate) { this(aname, ahiredate, 20.00); public HourlyEmployee3(String aname, Date ahiredate, double anhourlywage) { this(aname, ahiredate, anhourlywage, 160.0); public HourlyEmployee3(String aname, Date ahiredate, double anhourlywage, double amonthlyhours) { super(aname, ahiredate); disallowzeroesandnegatives(anhourlywage, amonthlyhours); hourlywage = anhourlywage; monthlyhours = amonthlyhours; CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 4 / 14

The Liskov Substitution Principle (LSP) Subtypes must be substitutable for their supertypes. Consider the method: public static Date vestdate(employee employee) { Date hiredate = employee.gethiredate(); int vestyear = hiredate.getyear() + 2; return new Date(vestYear, hiredate.getmonth(), hiredate.getday()); We can pass any subtype of Employee to this method: DateFormat df = DateFormat.getDateInstance(); HourlyEmployee eva = new HourlyEmployee("Eva L. Uator", df.parse("february 13, 2013"), 20.00, 200); Date evavestdate = vestdate(eva); We must ensure that subtypes are indeed substitutable for supertypes. CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 5 / 14

LSP Counterexample A suprising counter-example: public class Rectangle { public void setwidth(double w) {... public void setheight(double h) {... public class Square extends Rectangle { public void setwidth(double w) { super.setwidth(w); super.setheight(w); public void setheight(double h) { super.setwidth(h); super.setheight(h); We know from math class that a square is a rectangle. The overridden setwidth and setheight methods in Square enforce the class invariant of Square, namely, that width == height. CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 6 / 14

LSP Violation Consider this client of Rectangle: public void g(rectangle r) { r.setwidth(5); r.setheight(4); assert r.area() == 20; Client (author of g) assumes width and height are independent in r becuase r is a Rectangle. If the r passed to g is actually an instance of Square, what will be the value of r.area()? The Object-oriented is-a relationship is about behavior. Square s setwidth and setheight methods don t behave the way a Rectangle s setwidth and setheight methods are expected to behave, so a Square doesn t fit the object-oriented is-a Rectangle definition. Let s make this more formal... CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 7 / 14

Conforming to LSP: Design by Contract Require no more, promise no less. Author of a class specifies the behavior of each method in terms of preconditions and postconditions. Subclasses must follow two rules: Preconditions of overriden methods must be equal to or weaker than those of the superclass (enforces or assumes no more than the constraints of the superclass method). Postconditions of overriden methods must be equal to or greater than those of the superclass (enforces all of the constraints of the superclass method and possibly more). In the Rectangle-Square case the postcondition of Rectangle s setwidth method: assert((rectangle.w == w) && (rectangle.height == old.height)) cannot be satisfied by Square, which tells us that a Square doesn t satisfy the object-oriented is-a relationship to Rectangle. CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 8 / 14

LSP Conforming 2D Shapes public interface 2dShape { double area(); public class Rectangle implements 2dShape { public void setwidth(double w) {... public void setheight(double h) {... public double area() { return width * height; public class Square implements 2dShape { public void setside(double w) {... public double area() { return side * side; Notice the use of an interface to define a type. CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 9 / 14

Interfaces An interface represents an object-oriented type: a set of public methods (declarations, not definitions) that any object of the type supports. Recall the 2dShape interface: public interface 2dShape { double area(); You can t instantiate interfaces. So you must define a class that implements the interface in order to use it. Implementing an interface is similar to extending a class, but uses the implements keyword: public class Square implements 2dShape { public void setside(double w) {... public double area() { return side * side; Now a Square is-a 2dShape. CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 10 / 14

Interfaces Define a Type public interface 2dShape { double area(); This means that any object of type 2dShape supports the area method, so we can write code like this: public double calctotalarea(2dshape... shapes) { double area = 0.0; for (2dShape shape: shapes) { area += shape.area(); return area; Two kinds of inheritance: implementation and interface inheritance. extending a class means inheriting both the interface and the implementation of the superclass implementing an interface means inheriting only the interface, that is, the public methods CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 11 / 14

Default Methods in Interfaces CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 12 / 14

Conflict Resolution for Default Methods Superclasses win. Interfaces clash. CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 13 / 14

Static Methods in Interfaces CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 14 / 14