17
Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

Embed Size (px)

Citation preview

Page 1: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

Page 2: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

2

Types

0Primitive types0 boolean, int, double, etc.

0Classes are also types for variables0 Dillo (Yes, more dillos)0 Person0 Spreadsheet0 Etc.

0 Interfaces are also to be types for variables

Page 3: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

3

Who can extend classes?

0Classes0Abstract classes

Page 4: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

4

Who can implement interfaces?

0Classes0Abstract classes

Page 5: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

5

Objects

0What does an object get from its class type?0 Both fields and methods

0What does an object get from its superclass type?0 Both fields and methods

Page 6: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

6

Interfaces: A Promise

0A promise to implement certain methods

0Example:0 class A implements ImyInterface

0A needs to have all methods that are inside of ImyInterface0 class B extends A

0B also needs to have all methods that are inside of ImyInterface

Page 7: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

7

Interfaces: How You Assign Their Types to Objects

0For example, say you have these two classes:0 class A implements ImyInterface0 class B implements ImyInterface

0This is okay:0 ImyInterface myObjectA = new A();0 ImyInterface myObjectB = new B();

0This is not okay:0 ImyInterface myObject = new ImyInterface();

Page 8: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

8

Interfaces: What They Offer

0 Can refer to multiple classes as a common type

0 Example (same as before):0 class A implements ImyInterface0 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());

Page 9: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

9

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()?

Page 10: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

10

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.”

Page 11: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

11

Casting0Again, imagine you have these two lines of code:

1. ImyInterface myObject = new C();2. ((C)myObject).returnFalse();

0Casting makes Java do this type check at runtime rather than when compiling0 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.”

Page 12: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

12

Casting0 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

to0 Another goes around the cast AND the name of the object0 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();

Page 13: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

13

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}

Page 14: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

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: Dog myDog = new GoldenRetriever();

It’s okay to make this cast:(GoldenRetriever) myDog

Why? Because myDog was originally created as a GoldenRetriever

Page 15: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

15

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.

Page 16: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

16

Superclasses and Subclasses: What You Cannot Do With Castingclass 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.”

Page 17: Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

17

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.