1 / 172

Introduction to Computer Science

Introduction to Computer Science. Unit 12. Packages Polymorphism Interfaces Exceptions Streams – input/output. Packages. Java classes are placed into directories (or folders) on the computer The classes in each directory form a package

kishi
Download Presentation

Introduction to Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computer Science Unit 12 • Packages • Polymorphism • Interfaces • Exceptions • Streams – input/output

  2. Packages • Java classes are placed into directories (or folders) on the computer • The classes in each directory form a package • This helps organize classes, and also gives another way of controlling access among classes • Example: java.applet is the package of classes in subdirectory "applet" under the directory "java" (whose location varies depending on the system)

  3. Sample Packages that come with the Java API (continually evolving) java.applet Classes for implementing applets java.awt For graphics, windows, GUIs java.awt.event For AWT event-handling model java.awt.image Classes for image processing java.awt.peer Interface defs, platform-indep. GUIs java.io Classes for input and output java.lang Basic language classes (String, Math) java.net Classes for networking java.util Useful auxiliary classes, like Date

  4. Direct Use of Java API Class • To use, for example, the class Math in the package java.lang, it is possible to use the fully-qualified name:x = java.lang.Math.sqrt(3);

  5. Importing Classes • Of course, it's more convenient to do it the way we've been doing it, using the import statement; eitherimport package_name.class_name;orimport package_name.*;

  6. Importing import package_name.class_name;allows class_name to be used without giving the full package name import package_name.*;allows all classes in the package to be used without qualifying their names

  7. Example of import java.util.Date d = new java.util.Date( );java.awt.Point p = new java.awt.Point(1, 2);java.awt.Button b = new java.awt.Button( );can be abbreviated asimport java.util.Date;import java.awt.*;…Date d = new Date( );Point p = new Point(1, 2);Button b = new Button( );

  8. It's Always There • Java always assumes that the classes in java.lang (basic language classes, such as String and Math) are available • It's as if you have the statementimport java.lang.*;at the beginning of every program

  9. Packages You Define • All of the .class files placed in one directory belong to the same, unnamed package • To cause a class to be placed in a particular named package: • Put the .class file in the appropriate directory • Compile the class with the package statement, which must be the first non-comment line in the Java source file:package package-name;

  10. Classes in Different Packages • Classes in different packages obviously have different fully-qualified names • Classes in different packages also have different rules regarding the visibility of names

  11. Example:Visibility of Instance Variables • A public instance variable is visible to all other classes • A private instance variable is visible only to the methods of its class • If it is declared neither public nor private, then it has "package visibility"; it is visible to methods defined in other classes in the same package as its class • Same rules apply to static variables and instance and static methods

  12. Similarly for Classes • A class declared public is visible to all classes • A class not declared public is visible to the classes in its own package • A class cannot be declared private

  13. private, public, protected • Private variables and methods: no access by clients or by subclasses • Public variables and methods: accessed by clients and by subclasses • Default (no label): variables and methods have package visibility, accessible only to clients and subclasses in the same package

  14. Protected • We might want to give subclasses access to variables and methods without allowing clients to have access • That's the purpose of another category of accessibility: protected • Members declared protected are visible to other classes in the same package, and to subclasses in other packages, but not to clients in other packages

  15. Two Kinds of Visibility • Inherited Visibility and Direct Access Visibility:class A { int _x; …}class B extends A { … _x … // inherited visibility of x … A._x … // direct access visibility of x}

  16. Summary of Visibility Visibility public default protected private clients in D D D nonesame package clients in D none none nonedifferent package subclass in D & I D & I D & I nonesame package subclass in D & I none I nonedifferent package I = inherited access, D = direct access

  17. Using Protected • When there's a possibility that a class will have subclasses (and you want the attributes and methods to be usable in the inheritance hierarchy), you should use protected • Inherited protected variables and methods are considered to be protected members of the subclass (visible to further subclasses, and hidden from clients) • Inherited public variables and methods are considered to be public members of the subclass (visible to everyone)

  18. class PreciseTime extends Time {…public void printTime ( ) {if ((_hour == 0) && (_minute == 0)) System.out.print(“midnight”);else if ((_hour == 12) && (_minute == 0)) System.out.print(“noon”);else {if (_hour == 0) System.out.print(12);else if (_hour > 12) System.out.print(_hour - 12);else System.out.print(_hour);if (_minute < 10) System.out.print(“:0”+ _minute);else System.out.print(“:” + _minute);if (_second < 10) System.out.print(“:0”+ _second);else System.out.print(“:” + _second);if (_hour < 12) System.out.print(“AM”);else System.out.print(“PM”); } } } Now we overrideprintTime( )

  19. Doesn't Work • Problem is, it doesn't work: the new printTime accesses _hour and _minute, private instance variables of class Time • So we need to change the definition of class Time:class Time {protected int _hour, _minute;… } • Still no access to _hour and _minute from regular clients of Time, just to PreciseTime and other subclasses of Time

  20. Constructors for Subclasses • Java guarantees that a class’s constructor method is called whenever an instance of that class is created • It also guarantees that the constructor is called whenever an instance of any subclass is created • Therefore, every constructor method must call (explicitly or implicitly) its superclass constructor method

  21. Constructors for Subclasses • If we have an inheritance hierarchy: class B|subclass C of B|subclass D of C…subclass F of …|subclass G of F If a new object of class G is created, the constructors will be called in order B, C, D, … , F, G (i.e., G calls F, which calls E,…)

  22. Constructor Chaining • If the first statement in a constructor is not an explicit call to a constructor of the superclass (using super), or an explicit call to another constructor in the same class (using this), then Java implicitly inserts the call super( ) (i.e., super with no arguments) • Even if this is used to invoke another constructor in the same class, eventually some constructor will (explicitly or implicitly) invoke the superclass constructor • If the superclass has no zero-argument constructor, the implicit super( ) causes a compilation error

  23. super( ) • Therefore, if a superclass doesn't have a zero-argument constructor, the subclass must explicitly call the superclass's constructor with arguments:class C extends B {…public C (…) {super( B's constructor arguments );… }}

  24. super( ) • The call to super(…) must be the first statement in the body of C's constructor • The constructor for PreciseTime made use of this feature, calling super(h, m) to allow the Time constructor to do initialization public PreciseTime(int h, int m, int s) {super(h, m); _second = s; }

  25. Dynamic Binding • I said that a PreciseTime object is also (kind of) a Time object • It's a useful way of thinking about it, and it is the basis for a very powerful feature of Java, dynamic binding • A PreciseTime object can be used almost anywhere that a Time object can be used

  26. Several Passes at Polymorphism • Polymorphism means “taking many forms”: a reference of a given class can adapt take the form of any of its subclasses. • First we will look at the polymorphism and dynamic binding quickly – a few key facts • Then we will look at it in depth – this set of slides adapted (with permission) from:www.cc.gatech.edu/classes/AY2002/cs1322_spring/slides/ current/CS2_22_PolymorphismDynamicBinding.ppt

  27. This is legal, blurring the distinction of Time and PreciseTime • A Time variable (like dawn) can contain a reference to a PreciseTime object • More generally, a CCC variable can contain a reference to any object of any class in CCC's subclass hierarchy (down the tree – but not up the tree) Time dawn; dawn = new PreciseTime(3, 45, 30);

  28. There's an order to this Time dawn; dawn heap

  29. This is legal, blurring the distinction of Time and PreciseTime Time dawn; dawn = new PreciseTime(3, 45, 30); dawn Attributes: _hour = 3 _minute = 45 _second = 30Methods: … dawn heap

  30. So now what? • What happens if we send dawn the printTime( ) message?dawn.printTime( );printTime( ) is defined in both Time and PreciseTime classes • dawn was declared as a Time variable, but the actual object in dawn is a PreciseTime object • So the printTime( ) defined in the PreciseTime class will be used

  31. Static Binding of Methods • Before we had subclasses, life was simple • The Java compiler could always figure out, without running the program, which instance method would be invoked • The ability to know the exact method that will be invoked every time a message is sent is called static binding of methods

  32. Dynamic Binding • This is no longer true when we have subclasses • It is possible to specify in a program that there's an object of some class, B (i.e., the object is defined as being referenced by a variable of type B) • B has subclasses C and D • C and D each provide their own definition of method f ( ) B newObj; B C D

  33. Dynamic Binding x might come from the user • The actual creation of the object (C or D) only occursat runtime • Therefore, the compilercan't know, when the object newObj is sent f ( ), whether it will be handled by the C definition or the D definition; we can only know at runtime if (x == 7) newObj = new C( );else newObj = new D( ); newObj.f( ); Using f( ) from class C or D?

  34. Polymorphism Pass 2:More In Depth • Polymorphism is an important concept that is a part of Object Oriented Programming • We often would like to deal with a collection of various types of objects. We want to process members of that group in a generic way. Yet in the end, we’d like specific and appropriate behavior to still occur. • This set of slides adapted (with permission) from:www.cc.gatech.edu/classes/AY2002/cs1322_spring/slides/current/ CS2_22_PolymorphismDynamicBinding.ppt

  35. Polymorphism • Example: We have an array of animals, each of which is an object of one subclass out of several possible subclasses of Animal. The array is declared to have Animal as its element type. Now we’d like to process through the array and have each element invoke a makeNoise() method. • Luckily when a method call is made, the compiler isn’t too concerned about the specifics of the method being called. It’s question is: “is there a method with a matching signature?”

  36. Array of Animals Animal • Fill an array with Bird, Dog, Fish – but the array is an array of Animal extends extends extends Fish Bird Dog An array of Animal – we want to fill it with different instances of animals, but send the same message to each – they each will respond differently [0] [1] [2]

  37. Polymorphism class Animal { public voidmakeNoise ( ) { System.out.println("I am an animal."); } // of makeNoise } // of Animal class Fish extends Animal { public voidmakeNoise( ) { System.out.println("Glug glug gurgle gurgle"); } // of makeNoise } // of Fish class Bird extends Animal { public voidmakeNoise( ) { System.out.println("Tweet tweet flap flap"); } // of makeNoise } // of Bird

  38. Polymorphism (cont’d) class Dog extends Animal { public voidmakeNoise( ) { System.out.println("Sniff sniff woof woof"); } // of makeNoise public void bark( ) { System.out.println("Arf Arf"); } // of bark } // of Dog

  39. Polymorphism public class Driver { public static void main (String[ ] argv) { Animal[ ] animalArray = new Animal[3]; int index; animalArray[0] = new Bird( ); animalArray[1] = new Dog( ); animalArray[2] = new Fish( ); for (index = 0; index < animalArray.length; index++) { animalArray[index].makeNoise( ); } // of for } // of main } // of Driver Output: Tweet tweet flap flap Sniff sniff woof woof Glug glug gurgle gurgle the Animal class has makeNoise, so any member of the array can makeNoise

  40. Polymorphism and Dynamic Binding • Polymorphism & Dynamic Binding together insure that the correct makeNoise( ) method will always be called. • An object of a subclass can be substituted for its superclass, e.g., a bird for an animal. “A bird is a animal.” Yes. • The reverse is not true: can’t substitute superclass for a subclass, e.g., CANNOT substitute an animal for a bird. “An animal is a bird?” No. Not necessarily.

  41. instanceof • The keyword instanceof is used to ask an object if it is an instance of the specified class, e.g., "Is this particular animal of class Dog?" (d instanceof Dog) • It’s a boolean relation, returning true or false: if (d instanceof Dog) { …}

  42. Casting with Polymorphism public class Driver2 { public static void main(String[ ] argv) { Animal[ ] = animalArray[3]; Dog d; int i; animalArray[0] = new Bird( ); animalArray[1] = new Dog( ); animalArray[2] = new Fish( ); for (i = 0; i < animalArray.length; i++) if (animalArray[i] instanceof Dog){ d = (Dog) animalArray[i]; d.bark( ); } // if } // main } // Driver2 We cast before calling bark() because only dogs can bark. Not all Animals can execute the method

  43. Upcasting • Why didn’t we have to explicitly cast Bird, Dog and Fish to Animal when we put the instances into the array on the previous slide? • Because this is upcasting – casting from a derived class to a base class – and Java does it for us automatically • You can also write it explicitly if you want (no harm done)

  44. Casting a Superclass to a Subclass • Casting used here to give an object of a superclass the form of the appropriate subclass. If we just wrote: if (animalArray[i] instanceof Dog) { animalArray[i].bark(); } it would produce an error because objects of class Animal have no method called bark. So, we first cast the object that instanceof tells us is indeed a Dog object, as a Dog. if (animalArray[i] instanceof Dog) { d = (Dog) animalArray[i] d.bark( ); }

  45. Why is Casting Necessary Here? • If Java can determine that a given Animal is or is not a Dog (via instanceof), then why do we need to cast it to a Dog object before Java can recognize that it can bark? • Why can’t Java do it for us automatically? • Answer: the difference between compile-time and run-time type checking.

  46. Why is Casting Necessary Here? Sourcecode Compile Bytecode JVMInterpreter Programruns errors errors • Compile-time Errors: • Those that are discernable without the program executing. • Question of language legality: "Is this a legal statement?" e.g., • index = strName;Statement is not legal. • Run-time Errors: • Those that are discernable only when the program is running with actual data values. • Question of execution legality: • "Is it legal for this variable to have the actual value assigned to it?", e.g., • animalArray[<badIndex>] = someAnimal • Statement legal, but particular index value isn’t.

  47. Why is Casting Necessary Here? if (animalArray[i] instanceof Dog) { d = (Dog) animalArray[i]; d.bark( ); } if (animalArray[i]instanceof Dog) { animalArray[i].bark(); } • 1st line is legal. 2nd line isn’t (unless array has Dog).We can see that 1st line guarantees 2nd is legal. • Compiler cannot see inter-statement dependencies… unless compiler runs whole program with all possible data sets! • Runtime system could tell easily. . . BUT. . . We want most checking at compile-time for reasons of bothperformance and correctness. • Here, legality of each line of code can be evaluated at compile time. • Legality of each line discernable without worrying about inter-statement dependencies, i.e., each line can stand by itself. • Can be sure that code is legal (not sometimes-legal).A Good Use for Casting: • Resolving polymorphic ambiguities for the compiler.

  48. How Objects Are Created Dog d = new Dog(); 1. 2. 3. Object Object Object Animal Animal Animal Dog Dog Dog d d d Execution Time An implicit super() calls parent class constructor first. After all, a Dogis-a Animal, && is-a Object

  49. o Object Object Animal Animal Dog Dog a a Multiple References to Different Types of the Same Instance We can create new references that point to different types in the same block of memory. Animal a = new Dog(); Object o = a;

  50. o Object Animal Dog a Dynamic Binding When calling a method on a reference, the method must be present in the type (or inherited). However, the specific implementation called is determined at runtime. That’s ‘dynamic binding’. .toString() .toString() .toString() System.out.println(o.toString()); Dynamic binding provides runtime resolution to the most specific implementation possible.

More Related