DOC PREVIEW
Penn CIT 591 - Subclasses

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

SubclassesCompositionComposition vs. InheritanceInheritanceAssignmentAssignment IIAssignment IIIArrays of ObjectsWrappersWrapper constructorsMore wrapper constructorsWrapper “deconstructors”Additional wrapper methodsBack to arraysAuto-boxing and auto-unboxingequals and other methodsTypes and valuesSending messagesOverriding methodsOverriding methods IISome methods cannot be overriddenSome variables cannot be shadowedSome classes cannot be extendedSome classes cannot be instantiatedSome objects cannot be alteredYou can always be more specificDon’t change the superclassExtend, don’t modifyRelated style rules, IRelated style rules, IIRelated style rules, IIIRelated style rules, IVThe EndJan 14, 2019Subclasses2CompositionThe most common way to use one class within another is composition—just have a variable of that typeExamples:class LunarLanderGame { LunarLander lander = new LunarLander(); ...class MaxPlayer { String name; // String is a class Game game; // Game is a classComposition is suitable when one class is composed of objects from another class, or needs frequent reference to objects of another class3Composition vs. InheritanceInheritance is appropriate when one class is a special case of another classExample 1:class Animal { ... }class Dog extends Animal { ... }class Cat extends Animal { ... }Example 2:class Player { ... }class ComputerPlayer extends Player { ... }class HumanPlayer extends Player { ... }Use inheritance only when one class clearly specializes another class (and should have all the features of that superclass)Use composition in all other cases4Inheritanceclass Animal {int row, column; // will be inheritedprivate Model model; // inherited but inaccessibleAnimal( ) { ... } // cannot be inheritedvoid move(int direction) { ... } // will be inherited}class Rabbit extends Animal {// inherits row, column, move, but not constructor// model really is inherited, but you can’t access itint distanceToEdge; // new variable, not inheritedint hideBehindBush( ) { ... } // new method, not inherited}5AssignmentA member of a subclass is a member of the original class; a rabbit is an animal Animal animalBehindBush;Rabbit myRabbit;...animalBehindBush = myRabbit; // perfectly legal myRabbit = animalBehindBush; // not legal myRabbit = (Rabbit)animalBehindBush;// legal syntax, but requires a runtime check6Assignment II animalBehindBush = myRabbit; is legal--but why? int NUMBER_OF_ANIMALS = 8;Animal animals[ ] = new Animal[NUMBER_OF_ANIMALS];animals[0] = new Rabbit();animals[1] = new Seagull();animals[2] = new Snail();... for (int i = 0; i < NUMBER_OF_ANIMALS; i++) animals[i].move(); // legal if defined in Animal7Assignment IIIFrom previous slide:for (int i = 0; i < NUMBER_OR_ANIMALS; i++) animals[i].allowMove(); // legal if defined in AnimalBut:for (int i = 0; i < NUMBER_OR_ANIMALS; i++) { if (animals[i] instanceof Rabbit) { ((Rabbit)animals[i]).tryToHide(); }}Here, tryToHide() is defined only for rabbitsWe must check whether animals[i] is a rabbitWe must cast animals[i] to Rabbit before Java will allow us to call a method that does not apply to all AnimalsAfter the if test, you might think Java “knows” that animals[i] is a Rabbit—but it doesn’t8Arrays of ObjectsWhen you declare an array, you must specify the type of its elements: Animal animals[ ];However, Object is a type, so you can say: Object things[ ]; // declaration things = new Object[100]; // definitionYou can put any Object in this array: things[0] = new Fox();But (before Java 5) you cannot do this: things[1] = 5; // why not?9WrappersEach kind of primitive has a corresponding wrapper (or envelope) object:byte Byteshort Shortint Integer (not Int)long Longchar Character (not Char)boolean Booleanfloat Floatdouble Double10Wrapper constructorsEach kind of wrapper has at least one constructor:Byte byteWrapper = new Byte(byte value)Short shortWrapper = new Short(short value)Integer intWrapper = new Integer(int value)Long longWrapper = new Long(long value)Character charWrapper = new Character(char value )Boolean booleanWrapper = new Boolean(boolean value )Float floatWrapper = new Float(float value)Double doubleWrapper = new Double(double value)11More wrapper constructorsEvery wrapper type except Character has a constructor that takes a String as an argumentExample: Boolean b = new Boolean("true");The constructors for the numeric types can throw a NumberFormatException:Example: Integer i = new Integer("Hello");12Wrapper “deconstructors”You can retrieve the values from wrapper objects:byte by = byteWrapper.byteValue();short s = shortWrapper.shortValue();int i = intWrapper.intValue();long l = longWrapper.longValue();char c = charWrapper.charValue();boolean bo = booleanWrapper.booleanValue();float f = floatWrapper.floatValue();double d = doubleWrapper.doubleValue();13Additional wrapper methodsWrapper classes have other interesting featuresvariables:Integer.MAX_VALUE = 2147483647methods:Integer.toHexString(number)anyType.toString();14Back to arraysWhy bother with wrappers?Object[ ] things = new Object[100];Prior to Java 5, you cannot do this: things[1] = 5;But you can do this: things[1] = new Integer(5);You cannot do this: int number = things[1];But you can do this: int number = ((Integer)things[1]).intValue();15Auto-boxing and auto-unboxingSince version 5, Java will automatically box (wrap) primitives when necessary, and unbox (unwrap) wrapped primitives when necessaryYou can now do the following (where things is an array of Object) :things[1] = 5; instead ofthings[1] = new Integer(5);int number = (Integer)things[1]; instead ofint number = ((Integer)things[1]).intValue();but not int number = (int)things[1];16equals and other methodsSome methods, such as equals, take an Object parameterExample: if (myString.equals("abc")) { ... }JUnit's assertEquals(expected, actual) also takes objects as argumentsAuto boxing and unboxing, while convenient, can lead to some strange problems:Integer foo = new Integer(5);Integer bar = new Integer(5);Now: foo == 5 is true bar == 5 is true foo.equals(bar) is true foo.equals(5) is true foo == bar is false17Types and valuesA


View Full Document

Penn CIT 591 - Subclasses

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Subclasses
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Subclasses and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Subclasses 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?