
Understanding Java Types, Classes, and Interfaces
Explore the world of Java programming by delving into primitive types, classes like Dillo and Person, interfaces, object inheritance, and the promises and challenges presented by implementing interfaces in Java.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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<ImyInterface> myList = new LinkedList<ImyInterface>(); 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
Whats wrong with this? (2) interface ImyInterface { int returnTheNumberSeven(); } class C implements ImyInterface { ... boolean returnFalse() { ... } .... } ImyInterface myObject = new C(); myObject.returnFalse(); Java sees myObjectas being of type ImyInterface. Java only knows that objects of type ImyInterfacehave the method returnTheNumberSevenand 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 myObjectis of type ImyInterface. 0 In line #2 you re saying, No Java, I ve got this, I promise this object is of type Cspecifically. 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