1 / 16

CS 302 Week 14

CS 302 Week 14. Jim Williams, PhD. This Week. P3 Milestone 3 due Thursday Lab: Exceptions and File I/O Lecture: Inheritance, Interfaces For Next Week: Muddiest point Online Course Evaluations available https://aefis.wisc.edu. Hmmm. Assume: KitchenTool kt = new Microwave();

gwilliam
Download Presentation

CS 302 Week 14

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. CS 302 Week 14 Jim Williams, PhD

  2. This Week • P3 Milestone 3 due Thursday • Lab: Exceptions and File I/O • Lecture: Inheritance, Interfaces • For Next Week: • Muddiest point • Online Course Evaluations available • https://aefis.wisc.edu

  3. Hmmm... Assume: KitchenTool kt = new Microwave(); CuttingTool ct = new CuttingTool(); • Is kt instanceof Object? • Is kt instanceof CookingTool? • Is kt instanceof KitchenTool? • Is kt instanceof Oven? • Is kt instanceof Microwave? • Is ct instanceof CookingTool? • Is ct instanceof KitchenTool? • Is ct instanceof CuttingTool?

  4. Hmmm... Object o = new Microwave(); CuttingTool ct = new CuttingTool(); Knife k = new Knife(); If method turnOn() is defined in CookingTool, • can ct invoke this method? • can o invoke this method? • Is ((CookingTool)o).turnOn() legal? If method cut() is defined in CuttingTool 4. is ct.cut() legal? 5. is k.cut() legal?

  5. What prints out? class KT { public String toString() { return "kt"; } } class CT extends KT { } class Kitchen { public static void main(String [] args) { Object o = new CT(); System.out.println( o); } }

  6. What is wrong with? class Kitchen { public static void main(String [] args){ Object o = new CookingTool(); Oven oven = (Oven)o; } } class KitchenTool { } class CookingTool extends KitchenTool { }

  7. Hmmm... Stove s = new Stove(); Object o = new CookingTool(); KitchenTool kt = new KitchenTool(); Knife k = new Knife(); Assume CookingTool has an equals method, Which equals method will • s.equals( o) call? • o.equals( s) call? • kt.equals( o) call? • is s.toString() legal? • is o.toString() legal? • is k.toString() legal?

  8. What will print out? class Pop { public boolean equals(Object o) { System.out.print("O "); return false; } public boolean equals(Pop p) { System.out.print("P "); return false; } public static void main(String []args) { Pop p1 = new Pop(); Pop p2 = new Pop(); Object o = p1; p1.equals( p2); o.equals( p2); p2.equals(o); } }

  9. Abstract Classes & Interfaces • Interface (Java not User Interface) • Concrete classes (normal) • Abstract classes • Abstract methods • Protected visibility • Multiple Interface Inheritance • Comparable interface

  10. Interface vs Abstract Class vs Concrete Class abstract class Drg { abstract int count(); void print() { System.out.print(“hello”); } } class MyDrg extends Drg { int count() { return 3; } } interface Drg { int count(); //public abstract void print(); } class MyDrg implements Drg { int count() { return 3; } void print() { System.out.print(“hello”); } }

  11. What will print out? abstractclass Drg { abstractint count(); public void print() { System.out.print( count()); } } class MyDrg extendsDrg { int count() { return 3; } } //in main MyDrg d = new MyDrg(); d.print();

  12. A Zoo application We want a program to play animal sounds. Include classes: Pig, Goat, and Duck. Be able to call the same method, makeSound(), for each and each should make the appropriate sound. Should be able to easily extend with other classes later.

  13. Comparable Interface public interface Comparable<T> { int compareTo(T o); } https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

  14. What is the bug? class TrackCount extends IntList { int count = 0; public void add( int i) { this.count++; super.add( i); } public void add2( int i, int j) { this.count += 2; super.add2( i, j); } } class IntList { private ArrayList<Integer> list = new ArrayList<>(); public void add( int i) { list.add( new Integer(i)); } public void add2( int i, int j) { add( i); add( j); } } //TEST CODE TrackCount tc = new TrackCount(); tc.add(3); tc.add2( 2, 4); System.out.println( "tc.count=" + tc.count);

  15. What is the bug? public class Super { public Super() { overrideMe(); } public void overrideMe() { } } public final class Sub extends Super { private final Date date; Sub() { date = new Date(); } @Override public void overrideMe() { System.out.println(date); } } //Test Code Sub sub = new Sub(); sub.overrideMe(); From Joshua Bloch, Effective Java

  16. What does this do? public static void show( File folder, String depth) { for ( File file : folder.listFiles()) { System.out.println( depth + file.getName()); if ( file.isDirectory()) show( file, depth + " "); } } public static void main(String[] args) { show( new File("."), " "); }

More Related