Superclasses Subclasses Casting and Access Modifiers Types Primitive types boolean int double etc Classes are also types for variables Dillo Yes more dillos Person ID: 484470
Download Presentation The PPT/PDF document "Types, Implementing, Extending, Interfac..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Types, Implementing, Extending, Interfaces,
Superclasses
,
Subclasses, Casting, and Access ModifiersSlide2
Types
Primitive
typesboolean, int, double, etc.Classes are also types for variablesDillo (Yes, more dillos)PersonSpreadsheetEtc.Interfaces are also to be types for variables
2Slide3
Who can extend classes?
Classes
Abstract classes3Slide4
Who can implement interfaces?
Classes
Abstract classes4Slide5
Objects
What does an object get from its class type?
Both fields and methodsWhat does an object get from its superclass type?Both fields and methods5Slide6
Interfaces: A Promise
A promise to implement certain methods
Example:class A implements
ImyInterface
A
needs to have all methods that are inside of
ImyInterface
class B extends A
B
also needs to have all methods that are inside of
ImyInterface
6Slide7
Interfaces: How You Assign Their Types to Objects
For example, say you have these two classes:
class A implements
ImyInterface
class B implements
ImyInterface
This is okay:
ImyInterface
myObjectA
= new A();
ImyInterface
myObjectB
= new
B();
This is not okay:ImyInterface myObject = new ImyInterface();
7Slide8
Interfaces: What They Offer
Can refer to multiple classes as a common type
Example (same as before):class A
implements
ImyInterface
class B
implements
ImyInterface
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());
8Slide9
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()?
9Slide10
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.”
10Slide11
Casting
Again, imagine you have these two lines of code:
ImyInterface myObject
= new C
();
((C)
myObject
).
returnFalse
();
Casting makes Java do this type check at runtime rather than when compiling
Java sees line #1 above and says, “Okay, so
myObject
is of type
ImyInterface
.”
In line #2 you’re saying, “No Java, I’ve got this, I promise this object is of type C specifically.”11Slide12
Casting
One last note regarding two lines of code:
ImyInterface myObject
= new C
();
((C)
myObject
).
returnFalse
();
Note the two sets of parentheses:
(
(
C
)
myObject
)
.returnFalse();One goes around the name of the class you’re casting this object toAnother goes around the cast AND the name of the objectYou can then do the .returnFalse() callTo reiterate, this will make Java angry:(C)myObject.returnFalse();
And this will make Java happy:
((C)
myObject
).
returnFalse
();12Slide13
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
}
13Slide14
Superclasses
and Subclasses:
What You Can 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
}
If you did this:
Dog
myDog
= new
GoldenRetriever
();
It’s okay to make this cast:
(
GoldenRetriever
)
myDogWhy? Because myDog was originally created as a GoldenRetriever14Slide15
Superclasses
and Subclasses:
What You Can 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
}
If you did this:
GoldenRetriever
myDog
= new GoldenRetriever();
It’s okay to do this:
(Dog)
myDogWhy?Because myDog’s original type, GoldenRetriever, extends Dog.15Slide16
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 DogError 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
.”
16Slide17
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