Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers
Types 0 Primitive types 0 boolean, int, double, etc. 0 Classes are also types for variables 0 Dillo (Yes, more dillos) 0 Person 0 Spreadsheet 0 Etc. 0 Interfaces are also to be types for variables 2
Who can extend classes? 0 Classes 0 Abstract classes 3
Who can implement interfaces? 0 Classes 0 Abstract classes 4
Objects 0 What does an object get from its class type? 0 Both fields and methods 0 What does an object get from its superclass type? 0 Both fields and methods 5
Interfaces: A Promise 0 A promise to implement certain methods 0 Example: 0 class A implements ImyInterface 0 A needs to have all methods that are inside of ImyInterface 0 class B extends A 0 B also needs to have all methods that are inside of ImyInterface 6
Interfaces: How You Assign Their Types to Objects 0 For example, say you have these two classes: 0 class A implements ImyInterface 0 class B implements ImyInterface 0 This is okay: 0 ImyInterface myObjectA = new A(); 0 ImyInterface myObjectB = new B(); 0 This is not okay: 0 ImyInterface myObject = new ImyInterface(); 7
Interfaces: What They Offer 0 Can refer to multiple classes as a common type 0 Example (same as before): 0 class A implements ImyInterface 0 class B implements ImyInterface 0 I can then do: LinkedList myList = new LinkedList (); And this list can contain objects of class A, of class B, or both: myList.add(new A()); myList.add(new B()); 8
When interfaces get tricky: What’s wrong with this? interface ImyInterface { int returnTheNumberSeven(); } class C implements ImyInterface {... boolean returnFalse() {... }.... } If I try to do: ImyInterface myObject = new C(); Why will Java be upset if I write: myObject.returnFalse()? 9
What’s wrong with this? (2) interface ImyInterface { int returnTheNumberSeven(); } class C implements ImyInterface {... boolean returnFalse() {... }.... } ImyInterface myObject = new C(); myObject.returnFalse(); Java sees myObject as being of type ImyInterface. Java only knows that objects of type ImyInterface have the method returnTheNumberSeven and they don’t have a method called returnFalse. Casting could let you correct this problem: ((C)myObject).returnFalse(); Here I’ve told Java, “I promise that myObject is of type “ C ” and has this method.” 10
Casting 0 Again, imagine you have these two lines of code: 1. ImyInterface myObject = new C(); 2. ((C)myObject).returnFalse(); 0 Casting makes Java do this type check at runtime rather than when compiling 0 Java sees line #1 above and says, “Okay, so myObject is of type ImyInterface.” 0 In line #2 you’re saying, “No Java, I’ve got this, I promise this object is of type C specifically.” 11
Casting 0 One last note regarding two lines of code: 1. ImyInterface myObject = new C(); 2. ((C)myObject).returnFalse(); 0 Note the two sets of parentheses: ((C)myObject).returnFalse(); 0 One goes around the name of the class you’re casting this object to 0 Another goes around the cast AND the name of the object 0 You can then do the.returnFalse() call 0 To reiterate, this will make Java angry: 0 (C)myObject.returnFalse(); 0 And this will make Java happy: 0 ((C)myObject).returnFalse(); 12
Superclasses and Subclasses: Example class Dog { Dog (){}; /* Just leaving constructor empty for now. Wouldn’t be empty if I wanted to pass in arguments when making a new Dog.*/ // Methods would be here, if I added them } class GoldenRetriever extends Dog { GoldenRetriever(){}; /* Just leaving constructor empty for now. be empty if I wanted to pass in arguments when making a new GoldenRetriever. */ // Methods would be here, if I added them } 13
Superclasses and Subclasses: What You Can Do With Casting class Dog { Dog (){}; /* Just leaving constructor empty for now. Wouldn’t be empty if I wanted to pass in arguments when making a new Dog.*/ // Methods would be here, if I added them } class GoldenRetriever extends Dog { GoldenRetriever(){}; /* Just leaving constructor empty for now. be empty if I wanted to pass in arguments when making a new GoldenRetriever. */ // Methods would be here, if I added them } If you did this: Dog myDog = new GoldenRetriever(); It’s okay to make this cast: (GoldenRetriever) myDog Why? Because myDog was originally created as a GoldenRetriever 14
Superclasses and Subclasses: What You Can Do With Casting class Dog { Dog (){}; /* Just leaving constructor empty for now. Wouldn’t be empty if I wanted to pass in arguments when making a new Dog.*/ // Methods would be here, if I added them } class GoldenRetriever extends Dog { GoldenRetriever(){}; /* Just leaving constructor empty for now. be empty if I wanted to pass in arguments when making a new GoldenRetriever. */ // Methods would be here, if I added them } If you did this: GoldenRetriever myDog = new GoldenRetriever(); It’s okay to do this: (Dog) myDog Why? Because myDog ’s original type, GoldenRetriever, extends Dog. 15
Superclasses and Subclasses: What You Cannot Do With Casting class Dog { Dog (){}; /* Just leaving constructor empty for now. Wouldn’t be empty if I wanted to pass in arguments when making a new Dog.*/ // Methods would be here, if I added them } class GoldenRetriever extends Dog { GoldenRetriever(){}; /* Just leaving constructor empty for now. be empty if I wanted to pass in arguments when making a new GoldenRetriever. */ // Methods would be here, if I added them } This is not okay: Dog myDog = new Dog(); and then doing (GoldenRetriever) myDog Why? Because myDog was originally created as a Dog Error will say: java.lang.reflect.InvocationTargetException Caused by: java.lang.ClassCastException: Dog cannot be cast to GoldenRetriever Takeaway: You can only refer to classes as their types or their “supertypes.” 16
What To Remember About Access Modifiers Remember: Set all fields to private (as a default) unless there is a good reason to set it to protected or public. One more time: Set all fields to private (as a default) unless there is a good reason to set it to protected or public. 17