1 / 82

Java Lecture 5

Java Lecture 5. CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon. 13 X 11. Our Story So Far. Object Oriented Programming Features Encapsulation Reusability Adaptability Object Oriented Programming Benefits Generic "Drop In" Components

feleti
Télécharger la présentation

Java Lecture 5

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. Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon 13X11

  2. Our Story So Far • Object Oriented Programming Features • Encapsulation • Reusability • Adaptability • Object Oriented Programming Benefits • Generic "Drop In" Components • Modeling Real World Objects • Handling Collections Easily

  3. Our Story So Far • Java Language Syntax • Class Structure • Fields • Constructors • Methods • Accessors • Modifiers • Special Concepts • toString • main • static

  4. Our Story So Far • All objects are manipulated using references

  5. The Story of O CS Version

  6. The Story of O Object Oriented

  7. The Story of O • Operator Overloading int a = 3; int b = 2; int c; String x = "Hello "; String y = "World!"; String z; c = a + b; z = x + y;

  8. The Story of O • Other Overloading • Constructor public Student(String name, double gpa) public Student(String name) • Method public int max(int a, int b) public int max(int a, int b, int c) public int max(short a, short b)

  9. The Story of O • Overriding • Also called redefinition class A { int someMethod() } class B extends A int someMethod() }

  10. The Story of O Everything is an Object!

  11. Inheritance class Animal { String name; public Animal(String name) { this.name = name; } }

  12. Inheritance class Dog extends Animal { int fleas; public Dog(String name, int fleas) { super(name); this.fleas = fleas; } }

  13. Inheritance class Cat extends Animal { int hairBalls; public Cat(String name, int hairBalls) { super(name); this.hairBalls = hairBalls; } }

  14. Inheritance (Deceptive Diagram) Animal Dog Cat

  15. Inheritance (True Diagram) Object Animal Dog Cat

  16. Truth Be Known • Class Object exists • It defines lots of useful methods • e.g. toString • see the API • Thus every class is either • a direct subclass of Object (no extends) or • a subclass of a descendant of Object (extends) • So what?

  17. Motivation • Repetitive tasks • Collections of things (objects) • Baseball cards • Library items • Shapes • Animals • Vehicles • Students

  18. Motivation • Collections are seldom uniform • Desire method of holding a collection of dissimilar items • Need to change the type mismatch rules

  19. Recall float f; double d ; int i; String s; CokeMachine cm; f = d; // illegal d = f; // legal i = d; // illegal d = i; // legal

  20. Recall float f; double d ; int i; String s; CokeMachine cm; f = (float)d; // legal d = f; // legal i = (int)d; // legal d = i; // legal

  21. Recall float f; double d ; int i; String s; CokeMachine cm; s = cm; // illegal cm = s; // illegal

  22. Recall float f; double d ; int i; String s; CokeMachine cm; s = (String)cm; // illegal cm = (CokeMachine)s; // illegal

  23. Inheritance Changes the Rules Object Animal Dog Cat

  24. Inheritance Changes the Rules Object o; Animal a; Dog d = new Dog(); Cat c = new Cat(); d = c; // illegal a = c; // OK, a Cat is an Animal o = c; // OK, a Cat is an Object o = a; // OK, an Animal is an Object a = o; // Illegal, not all Objects are Animals d = a; // Illegal, not all animals are Dogs Confusing?

  25. The Keyword is Extends Object creation process Dog d = new Dog(); 1. Create reference d 2. Start creating Dog by entering Dog constructor and making call to parent. 3. Start creating Animal by entering Animal constructor and making call to parent. 4. Create Object portion 5. Create Animal portion 6. Create Dog portion

  26. Ref: Dog d Step by Step Dog d

  27. Ref: Dog d Step by Step Dog d = new Dog(); public Dog() { }

  28. Ref: Dog d Step by Step Dog d = new Dog(); public Dog() { } public Animal() { }

  29. Ref: Dog d Step by Step Dog d = new Dog(); public Dog() { } Object public Animal() { } public Object() { }

  30. Ref: Dog d Step by Step Dog d = new Dog(); public Dog() { } Object Animal public Animal() { }

  31. Ref: Dog d Step by Step Dog d = new Dog(); public Dog() { } Object Animal Dog

  32. Ref: Dog d Step by Step Dog d = new Dog(); Object Animal Dog

  33. Ref: Dog d Step by Step Dog d = new Dog(); Object Animal "A Dog Object" Dog

  34. Ref: Animal a Quizlette Animal a = new Dog(); Object Animal "A Dog Object" Dog

  35. Ref: Object o Quizlette Object o = new Dog(); Object Animal "A Dog Object" Dog

  36. Ref: Dog d Quizlette Dog d = new Animal(); Object Animal "An Animal Object" ILLEGAL

  37. Ref: Animal a Quizlette Animal a = new Object(); Object "An Object Object" ILLEGAL

  38. Quizlette Dog d = new Object(); ?

  39. Ref: Dog d Quizlette Dog d = new Object(); Object "An Object Object" ILLEGAL

  40. Ref: Dog Ref: Object Ref: Object Ref: Dog d o o d Same Logic d = o; ? o = d; OK! ?

  41. Don't be confused!!! • Primitives double d; float f; d = f; // legal...no loss of information f = d; // illegal...potential loss of // information

  42. Don't be confused!!! • References Object o; Dog d; o = d; // legal (a dog is an object) d = o; // illegal (all objects are not // dogs) Note: Not the same as primitives...they are all just references!!!

  43. Warning • When you break the aforementioned rules... • Java sometimes tells you that a cast is required • Even if it's a real bad idea Pearls p; Swine s; p = (Pearls)s;

  44. What's the Point? • Java Philosophy: Catch errors at compile time. • Leading to tricky concept #2: Dynamic Binding • At run time (dynamic) when a method is invoked on a reference the ACTUAL OBJECT is examined and the "lowest" or closest version of the method is actually run.

  45. Dynamic Binding • The heart of polymorphism • Assume Animal and Dog have a toString method Object o = new Dog(); Animal a = new Dog(); Dog d = new Dog(); o.toString(); a.toString(); d.toString(); ((Object)o).toString(); Object Object Animal Object Animal Dog

  46. Object toString() Animal Dog toString() Dynamic Binding • It even works like this... A Dog Object Ref: Animal a Animal a = new Dog(); a.toString();

  47. Trick #3 • Java checks types at compile time when assigning references (Run time checking is also performed). • Java always decides the method to be invoked by looking at the object at runtime. • At compile time Java checks method invocations to make sure that the reference type will have the correct method. This may appear contradictory to dynamic binding.

  48. Reference/Method Checking x.y(); • x is a reference which has a type which is its class • That class (or a superclass) must have method y or a compile error will result.

  49. Object toString() Object toString() Object toString() Animal move() Animal move() Animal move() Dog move() toString() bark() Dog move() toString() bark() Dog move() toString() bark() More Quizlette Fun Object o = new Dog(); Animal a = new Dog(); Dog d = new Dog(); o.toString(); o.move(); o.bark(); a.toString(); a.move(); a.bark(); d.toString(); d.move(); d.bark(); o a d

  50. Dog woof() Cat meow() Pig oink() Back to Collections • The simple approach Object

More Related