Download presentation
Presentation is loading. Please wait.
Published byLester Griffith Modified over 9 years ago
1
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
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 = 3 2
4
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)
5
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)
6
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)
7
Interfaces in Real World
8
Interfaces Can only declare important constant fields public static final must be used for fields Interface declares public abstract methods Methods callable anywhere by any class But method’s body cannot be defined in interface
9
Adding to an Interface Interfaces have sub-interfaces Sub-interface extends super-interface Super-interface’s methods included in sub-interface Methods from super-super-interface also inherited Sub-interface can extend multiple interfaces Methods not defined so no problems possible
10
Declaring Interfaces public interface Drawable { public void setColor(Color c); public Color getColor(); public void draw(Graphics g); } public interface TranDraw extends Drawable{ public void setTransparency(int tLevel); } public interface MoveDraw extends Drawable{ public void setPosition(int x, int y); }
11
Interfaces implement Classes implement interfaces Implementing classes define interface’s methods Any number of interfaces may be implemented Multiple inheritance issues ignored -- methods empty Unrelated to superclass chosen or used by a class Classes must implement all interface’s methods Includes methods inherited from super-interface
12
Implementing Interfaces public class Square implements Drawable { private Color c; private int x, y, side; public Square(Color col, int len) { c = col; side = len; } public void setColor(Color col){ c=col; } public Color getColor() { return c; } public void draw(Graphics g) { g.drawRect(x, y, side, side, c); } }
13
Implementing Interfaces public class Oval implements Drawable { private Color c; private int x, y, majRad, minRad; public Oval(Color co, int maj, int min){ c = co; majRad = maj; minRad = min; } public void setColor(Color col){ c=col; } public Color getColor() { return c; } public void draw(Graphics g) { g.drawOval(x, y, majRad, minRad, c); } }
14
Using Interfaces
17
Interface Overview Classes can implement interfaces instances Both used to mark instances 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
18
Typecasting int i = 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
19
Typecasting int i = 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
20
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 Drawable obj = new Oval(...);
21
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 Drawable obj = new Oval(...); Oval sad = obj; // Does not work
22
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 Drawable obj = new Oval(...); Oval sad = obj; // Does not work Oval glad = (Oval)obj; // works!
23
Your Turn Get into your groups and complete activity
24
For Next Lecture Read B.13 for Wed. and be ready to discuss What is inheritance? extends How does the extends keyword work? How does inheritance compare to interfaces? As usual, there is weekly assignment on Angel Tuesday Due by 5PM Tuesday via Assignment Submitter Each problem graded using provided JUnit tests
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.