Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Introduction to Java 2 Programming
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
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.
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.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
LECTURE 07 Programming using C# Inheritance
Inheritance using Java
Chapter 8 More Object Concepts
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
What is inheritance? It is the ability to create a new class from an existing class.
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
BY:- TOPS Technologies
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Inheritance and Polymorphism
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Interfaces.
Computer Science II Exam 1 Review.
this keyword this : A reference to the implicit parameter Syntax:
this keyword this : A reference to the implicit parameter Syntax:
Interfaces.
CMSC 202 Generics.
Go to pollev.com/cse143.
Summary.
CMPE212 – Reminders Assignment 2 due next Friday.
Software Specifications
Presentation transcript:

Inheritance (Part 3) 1

Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance (is-a)  a subclass is supposed to be able to do everything its superclasses can do  how do they interact? 2

Strength of a Precondition  to strengthen a precondition means to make the precondition more restrictive // Dog setEnergy // 1. no precondition // 2. 1 <= energy // 3. 1 <= energy <= 10 public void setEnergy(int energy) {... } 3 weakest precondition strongest precondition

Preconditions on Overridden Methods  a subclass can change a precondition on a method but it must not strengthen the precondition  a subclass that strengthens a precondition is saying that it cannot do everything its superclass can do 4 // Dog setEnergy // assume non-final none public void setEnergy(int nrg) { //... } // Mix setEnergy // bad : strengthen precond. 1 <= nrg <= 10 public void setEnergy(int nrg) { if (nrg 10) { // throws exception } //... }

 client code written for Dog s now fails when given a Mix  remember: a subclass must be able to do everything its ancestor classes can do; otherwise, clients will be (unpleasantly) surprised 5 // client code that sets a Dog's energy to zero public void walk(Dog d) { d.setEnergy(0); }

Postconditions and Inheritance  postcondition  what the method promises to be true when it returns  the method might promise something about its return value  "returns size where size is between 1 and 10 inclusive"  the method might promise something about the state of the object used to call the method  "sets the size of the dog to the specified size"  the method might promise something about one of its parameters  how do postconditions and inheritance interact? 6

Strength of a Postcondition  to strengthen a postcondition means to make the postcondition more restrictive // Dog getSize // 1. no postcondition // 2. 1 <= this.size // 3. 1 <= this.size <= 10 public int getSize() {... } 7 weakest postcondition strongest postcondition

Postconditions on Overridden Methods  a subclass can change a postcondition on a method but it must not weaken the postcondition  a subclass that weakens a postcondition is saying that it cannot do everything its superclass can do 8 // Dog getSize // 1 <= size <= 10 public int getSize() { //... } // Dogzilla getSize // bad : weaken postcond. 1 <= size public int getSize() { //... } Dogzilla: a made-up breed of dog that has no upper limit on its size

 client code written for Dog s can now fail when given a Dogzilla  remember: a subclass must be able to do everything its ancestor classes can do; otherwise, clients will be (unpleasantly) surprised 9 // client code that assumes Dog size <= 10 public String sizeToString(Dog d) { int sz = d.getSize(); String result = ""; if (sz < 4) result = "small"; else if (sz < 7) result = "medium"; else if (sz <= 10) result = "large"; return result; }

Exceptions  all exceptions are objects that are subclasses of java.lang.Throwable 10 Throwable Exception RuntimeException... and many, many more IllegalArgumentException... and many more AJ chapter 9

User Defined Exceptions  you can define your own exception hierarchy  often, you will subclass Exception 11 Exception DogException BadSizeExceptionNoFoodExceptionBadDogException public class DogException extends Exception

Exceptions and Inheritance  a method that claims to throw an exception of type X is allowed to throw any exception type that is a subclass of X  this makes sense because exceptions are objects and subclass objects are substitutable for ancestor classes // in Dog public void someDogMethod() throws DogException { // can throw a DogException, BadSizeException, // NoFoodException, or BadDogException } 12

 a method that overrides a superclass method that claims to throw an exception of type X must also throw an exception of type X or a subclass of X  remember: a subclass promises to do everything its superclass does; if the superclass method claims to throw an exception then the subclass must also // in public void someDogMethod() throws DogException { //... } 13

Which are Legal?  in public void someDogMethod() throws public void someDogMethod() throws public void public void someDogMethod() throws DogException, IllegalArgumentException 14

Review 1. Inheritance models the ______ relationship between classes. 2. Dog is a ______ of Object. 3. Dog is a ______ of Mix. 4. Can a Dog instance do everything a Mix instance can? 5. Can a Mix instance do everything a Dog instance can? 6. Is a Dog instance substitutable for a Mix instance? 7. Is a Mix instance substitutable for a Dog instance? 15

8. Can a subclass use the private attributes of its superclass? 9. Can a subclass use the private methods of its superclass? 10. Suppose you have a class X that you do not want anyone to extend. How do you enforce this? 11. Suppose you have an immutable class X. Someone extends X to make it mutable. Is this legal? 12. What do you need to do to enforce immutability? 16

13. Suppose you have a class Y that extends X. a. Does each X instance have a Y instance inside of it? b. How do you construct the Y subobject inside of the X instance? c. What syntax is used to call the superclass constructor? d. What is constructed first–the Y subobject or the X object? e. Suppose Y introduces a brand new method that needs to call a public method in X named xMethod. How does the new Y method call xMethod? f. Suppose Y overrides a public method in X named xMethod. How does the overriding Y method call xMethod? 17

14. Suppose you have a class Y that extends X. X has a method with the following value must be a multiple of 2 If Y overrides the method which of the following are acceptable preconditions for the overriding method: value must be a multiple of 2 value must be odd value must be a multiple of 2 and must be less than 100 value must be a multiple of 10 none 18

14. Suppose you have a class Y that extends X. X has a method with the following – A String of length 10 If Y overrides the method which of the following are acceptable postconditions for the overriding method: – A String of length 9 or 10 – The String "weimaraner" – An int – The same String returned by toString – A random String of length 10 19

15. Suppose Dog toString has the following Javadoc: /* * Returns a string representation of a dog. * The string is the size of the dog followed by a * a space followed by the energy. The string representation of the dog. */ Does this affect subclasses of Dog? 20