240 likes | 349 Views
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.
E N D
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
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
Interfaces in Real World • Everyone should give me colored paper in wallets • Men pictured on these unimportant papers • In return, will show you trip pictures (when I get back)
Interfaces in Real World • Everyone should give me colored paper in wallets • Men pictured on these unimportant papers • In return, will show you trip pictures (when I get back)
Interfaces in Real World • Everyone should give me colored paper in wallets • Men pictured on these unimportant papers • In return, will show you trip pictures (when I get back)
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
Adding to an Interface • Interfaces have sub-interfaces • Sub-interfaceextendssuper-interface • Super-interface’s methods included in sub-interface • Methods from super-super-interface also inherited • Sub-interface can extendmultiple interfaces • Methods not defined so no problems possible
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);}
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
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);} }
Implementing Interfaces public class Oval implementsDrawable {private Color c;private int x, y, majRad, minRad;public Oval(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);} }
Using Interfaces • Cannot instantiate an interface… • Not possible, since only create instances of class • Variables of interface type perfectly legal • Variable can refer to implementing class instance • Methods must be implemented in actual class • In Java, which method called by instance type public voiddrawRed(Drawabled, Graphics g) {d.setColor(Color.red);d.draw(g); }
Using Interfaces • Cannot instantiate an interface… • Not possible, since only create instances of class • Variables of interface type perfectly legal • Variable can refer to implementing class instance • Methods must be implemented in actual class • In Java, which method called by instance type public voiddrawRed(Drawabled, Graphics g) {d.setColor(Color.red);d.draw(g); }
Using Interfaces • Cannot instantiate an interface… • Not possible, since only create instances of class • Variables of interface type perfectly legal • Variable can refer to implementing class instance • Methods must be implemented in actual class • In Java, which method called by instance type public voiddrawRed(Drawabled, Graphics g) {d.setColor(Color.red);d.draw(g); }
Interface Overview • Classes can implementinterfaces • Both used to markinstances define method(s) • All variables can use interface or class as type • Abstact methods defined by interface • More flexible: class can implement many interfaces • Can mark classes; do not have to declare methods
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
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
Narrowing Conversions • Java cannot compile narrowing conversions • Assign interface variable to implementing class variable • Compiler will not allow it, but could be legal • Typecasting required for these assignments Drawableobj = new Oval(...);
Narrowing Conversions • Java cannot compile narrowing conversions • Assign interface variable to implementing class variable • Compiler will not allow it, but could be legal • Typecasting required for these assignments Drawableobj = new Oval(...); Oval sad = obj; // Does not work
Narrowing Conversions • Java cannot compile narrowing conversions • Assign interface variable to implementing class variable • Compiler will not allow it, but could be legal • Typecasting required for these assignments Drawableobj = new Oval(...); Oval sad = obj; // Does not work Oval glad = (Oval)obj; // works!
Your Turn • Get into your groups and complete activity
For Next Lecture • Read B.13 for Wed. and be ready to discuss • What is inheritance? • How does the extendskeyword work? • How does inheritance compare to interfaces? • As usual, there is weekly assignment on Angel • Due by 5PM Tuesdayvia Assignment Submitter • Each problem graded using provided JUnit tests