1 / 30

Question of the Day

Question of the Day. Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’) Should have equal values on both sides of equals sign Not limited to what is possible in Java. Question of the Day.

petula
Download Presentation

Question of the Day

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. Question of the Day • Write valid mathematical equation using:2 3 4 5oneaddition operator (‘+’)oneequality operator (‘=’) • Should have equal values on both sides of equals sign • Not limited to what is possible in Java

  2. Question of the Day • Write valid mathematical equation using:2 3 4 5 one addition operator (‘+’)one equality operator (‘=’) • Should have equal values on both sides of equals sign • Not limited to what is possible in Java 5 + 4 = 32

  3. Lecture 11:Abstract Classes & Interfaces

  4. Announcement

  5. Inheritance Issues • Multiple inheritance causes many problems • What if there are multiple superclasses declare field? • Which constructor will super()chain to? • Since classes extend only 1 class, Java avoids problem • Often mix multiple ideas within class, however

  6. Inheritance Issues • Multiple inheritance causes many problems • What if there are multiple superclasses declare field? • Which constructor will super() chain to? • Since classes extend only 1 class, Java avoids problem • Often mix multiple ideas within class, however public class Mammal { … } public class Bird { … } public class Platypus extends

  7. Inheritance Issues • Multiple inheritance causes many problems • What if there are multiple superclasses declare field? • Which constructor will super()chain to? • Since classes extend only 1 class, Java avoids problem • Often mix multiple ideas within class, however public class Mammal { … } public class Bird { … } public class Platypus extends

  8. abstract • Can declare methods as abstract • Methods cannot be defined; promise functionality • Methods can have parameters, return type, etc. • Bodies are illegal; declaration only lists signature public abstract void foo(inti);public abstract Beer bar(String o);

  9. abstract class • If we have a class containing abstract methods • Class considered abstractno matter how declared • abstractclasses similar to regular classes… • static & non-static fields allowed in class • Class will always extend exactly 1 other class • Mix of abstract& normal methods can be included • May use default one or maydefine a constructor • Can be extended by other classes

  10. abstract class • If we have a class containing abstract methods • Class considered abstractno matter how declared • abstractclasses similar to regular classes… • …but cannot instantiate abstract class • And abstractmethods inherited by subclasses

  11. abstract class • If we have a class containing abstract methods • Class considered abstractno matter how declared • abstractclasses similar to regular classes… • …but cannot instantiate abstract class • And abstractmethods inherited by subclasses • But subclass can override method & make it concrete

  12. Interfaces in Real World

  13. Interfaces • Can only declare important constant fields • publicstaticfinalmust be used for fields • Interface declares publicabstractmethods • Methods callable anywhere by any class • But method’s body cannot be defined in interface

  14. Adding to an Interface • Interfaces have sub-interfaces • Sub-interfaceextendssuper-interface • Super-interface’s methods inherited by sub-interface • Methods from super-super-interface also inherited • Sub-interface can extendmultiple interfaces • Methods not defined so does not cause problems

  15. Declaring Interfaces publicinterfaceDrawable{public void setColor(Color c); public ColorgetColor(); public void draw(Graphics g);} public interfaceTranDrawextendsDrawable{ public void setTransparency(inttLevel);} public interfaceMoveDrawextendsDrawable{ public void setPosition(int x, int y); }

  16. Interfaces • Classesimplementinterfaces • Implementing classes define interface’s methods • Any number of interfaces may be implemented • Multiple inheritance issues ignored -- methods empty • Unrelated to superclasschosen or used by a class • Classes must implement all interface’s methods • Includes methods inherited from super-interface

  17. Implementing Interfaces public class SquareimplementsDrawable {private Color c;private int x, y, side;public Square(Colorcol, intlen) { c = col; side = len;}public void setColor(Colorcol){ c=col; }public ColorgetColor() { return c; }public void draw(Graphics g) { g.drawRect(x, y, side, side, c);} }

  18. Implementing Interfaces public class Oval implementsDrawable {private Color c;private int x, y, majRad, minRad;public Square(Color co, intmaj, int min){ c = co;majRad = maj;minRad = min;}public void setColor(Color col){ c=col; }public ColorgetColor() { return c; }public void draw(Graphics g) { g.drawOval(x, y, majRad, minRad, c);} }

  19. Using Interfaces • Cannot instantiate an interface… • Not possible, since only create instances of class • Variables of interface type are perfectly legal • Variable can refer to implementing class instance • Methods must be implemented in actual class • Remember: which method called by instance type public voiddrawRed(Drawabled, Graphics g) {d.setColor(Color.red);d.draw(g); }

  20. Using Interfaces • Cannot instantiate an interface… • Not possible, since only create instances of class • Variables of interface type are perfectly legal • Variable can refer to implementing class instance • Methods must be implemented in actual class • Remember: which method called by instance type public voiddrawRed(Drawabled, Graphics g) {d.setColor(Color.red);d.draw(g); }

  21. Using Interfaces • Cannot instantiate an interface… • Not possible, since only create instances of class • Variables of interface type are perfectly legal • Variable can refer to implementing class instance • Methods must be implemented in actual class • Remember: which method called by instance type public voiddrawRed(Drawabled, Graphics g) {d.setColor(Color.red);d.draw(g); }

  22. Interface vs. Abstract Class • Both concepts serve similar purposes • Cannot instantiate either of these types • But can be used for variable, field, & parameter types • Used in other classes to identify important features • But very important differences define when each used

  23. Interface vs. Abstract Class • Canextendclasses & implementinterfaces • Both used to markinstances have methods defined • Fields, params, & locals can use either as their type • Use abstractclass when… • …classes should use common method or private field • …place in object hierarchy as subclass of existing class • Otherwise use interfacefor abstract methods • More flexible: class can implement many interfaces • Can mark classes; do not have to declare methods

  24. Typecasting inti = 13;Square s = ((Square)i); • Only exist to “assist” compiler with code • Changes variable’s type so compilation continues • Not in *.class file – does not affect instance at all • Only when KNOW instance & variables types differ • Errors at runtime instead of during compilation • Illegal code will compile, but still illegal

  25. Typecasting inti = 13;Square s = ((Square)i); • Only exist to “assist” compiler with code • Changes variable’s type so compilation continues • Not in *.class file – does not affect instance at all • Only when KNOW instance & variables types differ • Errors at runtime instead of during compilation • Illegal code will compile, but still illegal

  26. Narrowing Conversions • Java cannot compile narrowing conversions • Assigns superclass/interface to lower variable • Compiler will not allow it, but could be legal • Typecasting required for these assignments Object obj = new String(“bye”);

  27. Narrowing Conversions • Java cannot compile narrowing conversions • Assigns superclass/interface to lower variable • Compiler will not allow it, but could be legal • Typecasting required for these assignments Object obj = new String(“bye”); String sad = obj; //  Does not work

  28. Narrowing Conversions • Java cannot compile narrowing conversions • Assigns superclass/interface to lower variable • Compiler will not allow it, but could be legal • Typecasting required for these assignments Object obj = new String(“bye”); String sad = obj; //  Does not work String glad = (String)obj; //  works!

  29. Your Turn • Get into your groups and complete activity

  30. For Next Lecture • Review your OO concepts & understand ideas • Some fun & different problems for Monday’s quiz • Covers all material since last quiz (must know Java, too) • Focus will be on: inheritance, interfaces, abstract classes, typecasting,how to tell if calls are legal, … • There is weekly assignment problem on Angel • Due by 5PM Tuesday (via e-mail) • Problem #2 graded using the provided JUnit tests

More Related