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.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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,
Inheritance Chapter 8.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Inheritance using Java
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
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.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
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.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Interfaces and Polymorphism CS 162 (Summer 2009).
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Inheritance and Polymorphism
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
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.
Inheritance (Part 4) Abstract Classes.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Interfaces and Inheritance
Interfaces.
Computer Science II Exam 1 Review.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Interfaces.
Inheritance, Polymorphism, and Interfaces. Oh My
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance.
Advanced Java Programming
Java Inheritance.
Summary.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

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 the same type of exception 2. throw a subclass of the exception type 3. not throw an exception  it must not throw any new types of exceptions 1

// in Dog public void someDogMethod() throws DogException { //... } // in Mix; bad, don't do public void someDogMethod() throws DogException, IOException { //... } 2

// client // works if given a Dog instance but fails if // given a Mix instance that throws an IOException public void someClientMethod(Dog d) { try { d.someDogMethod(); } catch(DogException ex) { // deal with the exception } 3

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

Important Annoucement 2  written test scheduled for Wed April 29 has been moved to Fri May 01  material covered  up to Wed April 22 5

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 6

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 7

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? 8

Polymorphism  inheritance allows you to define a base class that has attributes and methods  classes derived from the base class can use the public and protected base class attributes and methods  polymorphism allows the implementer to change the behaviour of the derived class methods 9

// client code public void print(Dog d) { System.out.println( d.toString() ); } // later on... Dog fido = new Dog(); CockerSpaniel lady = new CockerSpaniel(); Mix mutt = new Mix(); this.print(fido); this.print(lady); this.print(mutt); 10 Dog toString CockerSpaniel toString Mix toString

// client code public void print(Dog d) { System.out.println( d.toString() ); } // later on... Dog fido = new Dog(); Dog lady = new CockerSpaniel(); Dog mutt = new Mix(); this.print(fido); this.print(lady); this.print(mutt); 11 Dog toString CockerSpaniel toString Mix toString

// client code public void print(Object obj) { System.out.println( obj.toString() ); } // later on... Dog fido = new Dog(); Dog lady = new CockerSpaniel(); Dog mutt = new Mix(); this.print(fido); this.print(lady); this.print(mutt); this.print(new Date()); 12 Dog toString CockerSpaniel toString Mix toString Date toString

Late Binding  polymorphism requires late binding of the method name to the method definition  late binding means that the method definition is determined at run-time 13 obj.toString() non-static method run-time type of the instance obj

 the declared type of an instance determines what methods can be used  the name lady can only be used to call methods in Dog  lady.someCockerSpanielMethod() won't compile  the actual type of the instance determines what definition is used when the method is called  lady.toString() uses the CockerSpaniel definition of toString 14 Dog lady = new CockerSpaniel();

Abstract Classes  often you will find that you want the API for a base class to have a method that the base class cannot define  e.g. you might want to know what a Dog 's bark sounds like but the sound of the bark depends on the breed of the dog  you want to add the method bark to Dog but only the subclasses of Dog can implement bark  e.g. you might want to know the breed of a Dog but only the subclasses have information about the breed  you want to add the method getBreed to Dog but only the subclasses of Dog can implement getBreed 15

 if the base class has methods that only subclasses can define and the base class has attributes common to all subclasses then the base class should be abstract  if you have a base class that just has methods that it cannot implement then you probably want an interface  abstract :  (dictionary definition) existing only in the mind  in Java an abstract class is a class that you cannot make instances of 16

 an abstract class provides a partial definition of a class  the subclasses complete the definition  an abstract class can define attributes and methods  subclasses inherit these  an abstract class can define constructors  subclasses can call these  an abstract class can declare abstract methods  subclasses must define these (unless the subclass is also abstract) 17

Abstract Methods  an abstract base class can declare, but not define, zero or more abstract methods  the base class is saying "all Dog s can provide a String describing the breed, but only the subclasses know enough to implement the method" 18 public abstract class Dog { // attributes, ctors, regular methods public abstract String getBreed(); }

public class Mix extends Dog { // stuff from public String getBreed() { if(this.breeds.isEmpty()) { return "mix of unknown breeds"; } StringBuffer b = new StringBuffer(); b.append("mix of"); for(String breed : this.breeds) { b.append(" " + breed); } return b.toString(); } 19

PureBreed  a purebreed dog is a dog with a single breed  one String attribute to store the breed  note that the breed is determined by the subclasses  the class PureBreed cannot give the breed attribute a value  but it can implement the method getBreed  the class PureBreed defines an attribute common to all subclasses and it needs the subclass to inform it of the actual breed  PureBreed is also an abstract class 20

public abstract class PureBreed extends Dog { private String breed; public PureBreed(String breed) { super(); this.breed = breed; } public PureBreed(String breed, int size, int energy) { super(size, energy); this.breed = breed; } 21

@Override public String getBreed() { return this.breed; } 22

Komondor public class Komondor extends PureBreed { private final String BREED = "komondor"; public Komondor() { super(BREED); } public Komondor(int size, int energy) { super(BREED, size, energy); } // other Komondor methods... } 23