SubclassesInheritanceAssignmentAssignment IIAssignment IIIArrays of ObjectsWrappersWrapper constructorsMore wrapper constructorsWrapper “deconstructors”Additional wrapper methodsBack to arraysTypes 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 alteredRule 70Rule 70, IIRule 70, IIIRelated style rules, IRelated style rules, IIRelated style rules, IIIRelated style rules, IVThe EndSubclassesInheritanceclass Animal {int row, column; // will be inheritedprivate Model model; // private prevents inheritanceAnimal( ) { ... } // cannot be inheritedvoid move(int direction) { ... } // will be inherited}class Rabbit extends Animal {// inherits row, column, move, but not model or constructorint distanceToEdge; // new variable, not inheritedint hideBehindBush( ) { ... } // new method, not inherited}Assignment•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 checkAssignment II animalBehindBush = myRabbit; is legal--but why? int NUMBER_OR_ANIMALS = 8;Animal animals[ ] = new Animal[NUMBER_OR_ANIMALS];animals[0] = new Fox();animals[1] = new Rabbit();animals[2] = new Deer();... for (int i = 0; i < NUMBER_OR_ANIMALS; i++) allowMove(animals[i]); // legal if defined in AnimalAssignment III•From previous slide:for (int i = 0; i < NUMBER_OR_ANIMALS; i++) allowMove(animals[i]); // 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 AnimalsArrays 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 you cannot do this: things[1] = 5; // why not?Wrappers•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 DoubleWrapper constructors•Each kind of wrapper has at least one constructor:–byte new Byte(byte value)–short new Short(short value)–int new Integer(int value)–long new Long(long value)–char new Character(char value)–boolean new Boolean(boolean value)–float new Float(float value)–double new Double(double value)More wrapper constructors•Every wrapper type except Character has a constructor that takes a String as an argument–Example: Boolean b = new Boolean("true");•These constructors for the numeric types can throw a NumberFormatException:–Example: Integer i = new Integer("Hello");Wrapper “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();Additional wrapper methods•Wrapper classes have other interesting features–variables:•Integer.MAX_VALUE = 2147483647–methods:•Integer.toHexString(number)•anyType.toString();Back to arrays•Why bother with wrappers?•Object[ ] things = new Object[100];•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();Types and values•A variable has both a type and a value•Consider Animal animal;–The type of variable animal is Animal•The type of a variable never changes•The syntax checker can only know about the type–The value of animal might sometimes be a rabbit and at other times be a fox•Messages such as animal.decideMove() are sent to the value•The value (object) determines which method to useSending messages•Java must ensure that every message is legal–That is, the object receiving the message must have a corresponding method•But when the Java compiler checks syntax, it can’t know what the value of a variable will be; it has to depend on the type of the variable–If the variable is of type T, then either•Class T must define an appropriate method, or•Class T must inherit an appropriate method from a superclass, or•Class T must implement an interface that declares an appropriate methodOverriding methodsclass Animal {int decideMove( ) {return Model.STAY;}}class Rabbit extends Animal {// override decideMoveint decideMove( ) { // same signature return random(Model.MIN_DIRECTION, Model.MAX_DIRECTION);}}Overriding methods II•When you override a method:–You must have the exact same signature–Otherwise you are just overloading the method, and both versions of the method are available•When you override a method, you cannot make it more private–In this example, Animal defines a method–Every subclass of Animal must inherit that method, including subclasses of subclasses–Making a method more private would defeat inheritanceSome methods cannot be overriddenclass Animal {final boolean canMove(int direction) { ... }}class Rabbit extends Animal {// inherits but cannot override canMove(int)}Some variables cannot be shadowed•class BorderLayout { public static final String NORTH = "North";•If you were to create a subclass of BorderLayout, you would not be able to redefine NORTHSome classes cannot be extended•final class StringContent { ... }•When an entire class is made final, it cannot be extended•Making a class final allows some extra optimizations•Very few Java-supplied classes are finalSome classes cannot be instantiated•TextComponent has
View Full Document