1 / 18

Java Interfaces and Abstract Classes (3)

Java Interfaces and Abstract Classes (3). Chris Loftus. Use of abstract class instead. Q. Was the Animal interface the best solution for our Pets problem? A. An abstract class is probably better since we still have the duplication problem

lars-barton
Download Presentation

Java Interfaces and Abstract Classes (3)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Interfaces and Abstract Classes (3) Chris Loftus

  2. Use of abstract class instead • Q. Was the Animal interface the best solution for our Pets problem? • A. An abstract class is probably better since we still have the duplication problem • We can put any duplicated code in the abstract superclass...

  3. UML class diagram using an Abstract class -myPets * Pets -myPets: Pet[] -numPets: int +addPet(newPet: Pet):void +showSound():void TestPets +main(…) {abstract} Pet - name: String +Pet() +getName():String {final} +sound():String Cat +sound():String … Fish +getKind():String … Dog +sound():String … HappyCat +HappyCat() +sound():String

  4. Demo (notes) • Pet is now an abstract class • Abstract means we cannot create an object of this class, cannot use new Pet() • I want to create instances of Cats, Dogs etc but not the abstract concept of Pet • Pet is used to store the pet name and provide getName and sound methods in one place... • Why use private for name rather than protected?...

  5. Demo (notes) • Pet constructor takes a name • Let’s see how this is used in the Cat subclass... • Default implementations for getName and sound • getName is final: it cannot be overridden in subclass. Let’s try... • sound default implementation means we won’t have to override in Fish (or other mute animals)

  6. Demo (notes) • Can also make methods abstract • I demonstrated this with sound... • Rather like methods in an interface so can still act like a contract... • Notice how Fish now does not compile since it must have a sound implementation...

  7. Abstract classes vs interfaces • Use abstract classes instead of or as well as interfaces if you have default implementations and somewhere to store common data • Interfaces are good when you don’t know what the implementation will look like and want to specify a contract that future implementation classes (including abstract classes) must meet...

  8. Demo (notes): HappyCat • This subclasses Cat • The HappyCatsound method also calls the sound method in the Cat superclass, by using super.sound(), appending extra happy cat sounds! • Let’s look at the overall object diagram on the whiteboard...

  9. Polymorphism • Means many forms • One of the four key aspects of OO: • abstraction, inheritance, encapsulation and polymorphism • Within OO means: • Allows a variable or method parameter to be written to take an object of type T, but will also be able to take an object of type S, where S is a subtype of T

  10. Examples -myPets {abstract} Pet * S <: T (S is a subtype of T) T >: S (T is a supertype of S) S v1; T v2 = v1; // Is allowed Q: Given the UML class diagram is this allowed? Pet p = new Fish(); Cat c = p; Cat Fish Dog HappyCat

  11. Other ways of saying it • We can always assign from a variable of the same type or a subtype to a variable of the same type or super-type • Left hand side of assignment must be the same type or a super-type of the right hand side of the assignment

  12. Liskov Substitution Principle • Polymorphism as shown above is also called the Liskov Substitution Principle after Barbara Liskov in her 1993 paper: • Family values: A behavioural notion of subtyping, MIT Technical Report, TR-562b

  13. Polymorphism and overriding • Subclasses can override a superclass’s non-final methods • At runtime the JVM works out which version of the method to run, and it’s always the most specialised method • E.g. Pet pet = new HappyCat(...); pet.sound(); // Which sound method is called (next slide)?

  14. Study the following code and class diagram:Pet pet = new HappyCat(...);pet.sound();Which sound method is called? (choose one) <<abstract>> Pet --------------------- +sound(): String ... • Pet.sound • Cat.sound • HappyCat.sound • I don’t know Cat --------------------- +sound(): String +getBreed(): String ... HappyCat --------------------- +sound(): String ... • Let’s draw an object diagram on the whiteboard to illustrate

  15. Yes or NoPet pet = new HappyCat(...); {abstract} Pet --------------------- +sound(): String ... Can I now call pet.getBreed()? Cat --------------------- +sound(): String +getBreed(): String ... HappyCat --------------------- +sound(): String ...

  16. Polymorphism and overriding • What do we need to do to access the getBreed() method? • We use a cast operator Pet pet; // pet = ... Assume some code here Cat c = (Cat)pet; c.getBreed(); • What could go wrong here at runtime? (next slide)...

  17. {abstract} Pet --------------------- +sound(): String ... Given the code fragment and UML class diagram:1. Pet pet;2. // 3. Cat c = (Cat)pet;4. c.getBreed();What could go wrong at runtime?(choose one) Fish --------------------- +sound(): String +getKind(): String ... Cat --------------------- +sound(): String +getBreed(): String ... • Nothing can go wrong • The variable pet might actually be a reference to a Fish causing an exception to be thrown at line 4 • The variable pet might actually be a reference to a Fish causing an exception to be thrown at line 3 • I don’t know HappyCat --------------------- +sound(): String ...

  18. More on casting • What if I did the following? Object var = new String(“Hello”); Cat c = (Cat)var; • If the JVM allowed this assignment what could go wrong?... • More on casting in a later talk...

More Related