cosc121:$$ computer$programming$iipublic abstract class animal { protected int numlegs; protected...

45
COSC 121: Computer Programming II Dr. Bowen Hui University of Bri?sh Columbia Okanagan 1

Upload: others

Post on 14-Aug-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

COSC  121:    Computer  Programming  II  

Dr.  Bowen  Hui  University  of  Bri?sh  Columbia  

Okanagan  

1  

Page 2: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Quick  Review  

•  Inheritance  models  IS-­‐A  rela?onship  •  Different  from  impor?ng  classes  •  Inherited  classes  can  be  organized  in  a  class  hierarchy  •  Special  classes  to  model  generic  concepts  that  do  not  get  instan?ated  are  called  abstract  classes  

2  

Page 3: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

public abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs() { // statements to define how fast it runs based on speed } }

Abstract  Class  Example  

3  

Page 4: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

public abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs() { // statements to define how fast it runs based on speed } }

Abstract  Class  Example  

•  Abstract  methods  end  with  semicolon  •  All  subclasses  must  define  walks()  and  eats()  or  declare  them  abstract  too  

4  

no  constructor      

   

   

Page 5: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Problem  of  Mul?ple  Inheritance  

•  Recall  Java  does  not  allow  this:  

5  

Vehicle

Car Truck

PickupTruck

Page 6: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Problem  of  Mul?ple  Inheritance  

•  Recall  Java  does  not  allow  this:  

6  

Vehicle

Car Truck

PickupTruck

Car  inherits  from  Vehicle  Truck  inherits  from  Vehicle  

PickupTruck  cannot  inherit    from  more  than  one  class  

Page 7: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Origin  of  Interfaces  

•  An  OOP  programming  technique  that  lets  you  “conform  to”  mul?ple  classes  

•  Avoids  the  collision  problem  that  arises  in  mul?ple  inheritance  – Don’t  have  any  a\ributes  

•  A  Java  interface  is  a  group  of  constants  and  abstract  methods  – All  methods  in  an  interface  must  be  abstract  – Reserved  word  abstract  is  not  needed  

7  

Page 8: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Example  

•  Example  of  an  interface:  

8  

public interface Doable { public void doThis(); public int doThat(); public void doThis2( double value, char ch ); public boolean doTheOther( int num ); }

Page 9: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Example  

•  Example  of  an  interface:  

•  What  does  this  example  remind  you  of?  

9  

public interface Doable { public void doThis(); public int doThat(); public void doThis2( double value, char ch ); public boolean doTheOther( int num ); } none  of  the  methods  

have  body  defini?ons  

   

no  constructor  

Page 10: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Using  an  Interface  

•  Suppose  you  want  to  define  different  types  of  memory  storage  components  e.g.,  CD,  USB  key,  etc.  

•  You  have:  

10  

Page 11: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Implemen?ng  an  Interface  

11  

Page 12: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Implemen?ng  an  Interface  

12  

   

can  have  a\ributes  

have  constructor  

all  abstract  methods  must  be  defined  

Page 13: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

About  Interfaces  

•  An  interface  cannot  be  instan?ated  •  All  methods  are  public abstract –  Even  if  reserved  words  not  used  

•  All  methods  must  be  defined  by  classes  that  implement  the  interface  

•  To  implement  an  interface:  – Use  reserved  word  implements – Define  all  the  methods  from  the  interface  –  Can  define  addi?onal  a\ributes  and  methods  

13  

Page 14: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

About  Interfaces  

•  An  interface  cannot  be  instan?ated  •  All  methods  are  public abstract –  Even  if  reserved  words  not  used  

•  All  methods  must  be  defined  by  classes  that  implement  the  interface  

•  To  implement  an  interface:  – Use  reserved  word  implements – Define  all  the  methods  from  the  interface  –  Can  define  addi?onal  a\ributes  and  methods  

14  

Page 15: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Interfaces  vs.  Abstract  Classes  

•  Interfaces:  – No  a\ributes  – All  method  are  abstract  – All  methods  are  public

•  Abstract  classes:  – Can  have  a\ributes  – Can  have  methods  with  body  defini?ons  – Methods  can  have  different  visibility  

15  

Page 16: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Interfaces  vs.  Abstract  Classes  

•  Interfaces:  – No  a\ributes  – All  method  are  abstract  – All  methods  are  public

•  Abstract  classes:  – Can  have  a\ributes  – Can  have  methods  with  body  defini?ons  – Methods  can  have  different  visibility  

16  

Page 17: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

When  to  Use  Which?  •  Interfaces:  – Want  to  guarantee  another  class  will  implement  a  set  of  methods  

–  Choose  when:  want  to  specify  method  signatures  to  enforce  compliance  •  Designed  for  standardizing  communica?on  across  classes  

•  Abstract  classes:  – No  guarantee  a  subclass  will  override  an  abstract  method  

–  Choose  when:  want  subclasses  to  have  a  default  behaviour  •  Designed  for  conceptual  modeling  and  to  maximize  reuse  

17  

Page 18: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Predefined  Interfaces  

•  Interfaces  other  people  defined  •  Comparable  interface  – Contains  one  abstract  method  called  compareTo() – Used  to  compare  two  objects  – How  to  call  it:  o1.compareTo( o2 ) where  o1  and  o2  are  the  same  types  

– Looks  just  like  any  other  method  call  with  an    object  passed  in  

18  

Page 19: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

The  Comparable  Interface  

•  Any  class  can  implement  Comparable •  Output  type:  int •  Output  is  …  –  nega?ve  if  obj1  “is  less  than”  obj2 –  0  if  the  two  are  equal  –  posi?ve  if  obj1  “is  greater  than”  obj2

•  E.g.:  if( obj1.compareTo( obj2 ) < 0 ) System.out.println( “obj1 is less than obj2” );

•  Your  class  gives  meaning  to  less/greater  than  

19  

Page 20: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

String  implements  Comparable

•  Recall  from  Ch  5  that  String  class  lets  us  compare  strings  by  lexicographic  order  

•  E.g.,  “abc”  <  “bcd”  •  You  write:  

String one = “abc”; String two = “bcd”; if( one.compareTo( two ) < 0 ) System.out.println( one + “ is less than ” + two );

20  

Page 21: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Defining  compareTo() •  Required  header  defini?on:  public int compareTo( Object o2 )

•  How  come  we  were  able  to  write:  one.compareTo( two )  where  one  and  two  are  Strings?  

•  Makes  use  of  polymorphism  (next  class)  •  Inside  compareTo(),  need  to  cast  types  –  Explicitly  tells  Java  what  type  it  really  is  –  E.g.,  for  the  String  class,  you  would  have:    public  int  compareTo(  Object  o2  )  {        String  other  =  (  String  )o2;  }  

21  

Page 22: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Defining  compareTo() •  Required  header  defini?on:  public int compareTo( Object o2 )

•  How  come  we  were  able  to  write:  one.compareTo( two )  where  one  and  two  are  Strings?  

•  Makes  use  of  polymorphism  (next  class)  •  Inside  compareTo(),  need  to  cast  types  –  Explicitly  tells  Java  what  type  it  really  is  –  E.g.,  for  the  String  class,  you  would  have:    public  int  compareTo(  Object  o2  )  {        String  other  =  (  String  )o2;  }  

22  

   

Page 23: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Defining  compareTo() •  Required  header  defini?on:  public int compareTo( Object o2 )

•  How  come  we  were  able  to  write:  one.compareTo( two )  where  one  and  two  are  Strings?  

•  Makes  use  of  polymorphism  (next  class)  •  Inside  compareTo(),  need  to  cast  types  –  Explicitly  tells  Java  what  type  it  really  is  –  E.g.,  for  the  String  class,  you  would  have:    public  int  compareTo(  Object  o2  )  {        String  other  =  (  String  )o2;  }  

23  

Page 24: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Defining  compareTo() •  Required  header  defini?on:  public int compareTo( Object o2 )

•  How  come  we  were  able  to  write:  one.compareTo( two )  where  one  and  two  are  Strings?  

•  Makes  use  of  polymorphism  (next  class)  •  Inside  compareTo(),  need  to  cast  types  –  Explicitly  tells  Java  what  type  it  really  is  –  E.g.,  for  the  String  class,  you  would  have:    public  int  compareTo(  Object  o2  )  {        String  other  =  (  String  )o2;  }  

24  

Page 25: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Defining  compareTo() •  Required  header  defini?on:  public int compareTo( Object o2 )

•  How  come  we  were  able  to  write:  one.compareTo( two )  where  one  and  two  are  Strings?  

•  Makes  use  of  polymorphism  (next  class)  •  Inside  compareTo(),  need  to  cast  types  –  Explicitly  tells  Java  what  type  it  really  is  –  E.g.,  for  the  String  class,  you  would  have:    public  int  compareTo(  Object  o2  )  {        String  other  =  (  String  )o2;  }  

25  

    says  “o2  really  is  a  String  type”  

Page 26: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Shoe  Size  Comparison  

26  

•  Compare  two  shoe  sizes  based  on  the  following  chart:  

•  Note:  Men  sizes  are  +2  to  women’s  

Page 27: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Define  Shoe  class  

27  

•  Which  a\ributes  will  you  keep  track  of?  – size – gender

•  Which  methods  do  you  need?  – Shoe() – compareTo()

•  How  to  access  other  object’s  private  a\ributes?  – getSize() – getGender()

Page 28: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Define  Shoe  class  

28  

•  Which  a\ributes  will  you  keep  track  of?  – size – gender

•  Which  methods  do  you  need?  – Shoe() – compareTo()

•  How  to  access  other  object’s  private  a\ributes?  – getSize() – getGender()

Page 29: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Define  Shoe  class  

29  

•  Which  a\ributes  will  you  keep  track  of?  – size – gender

•  Which  methods  do  you  need?  – Shoe() – compareTo()

•  How  to  access  other  object’s  private  a\ributes?  – getSize() – getGender()

Page 30: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Define  Shoe  class  

30  

•  Which  a\ributes  will  you  keep  track  of?  – size – gender

•  Which  methods  do  you  need?  – Shoe() – compareTo()

•  How  to  access  other  object’s  private  a\ributes?  – getSize() – getGender()

Page 31: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Sample  Solu?on  

31  

Page 32: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Sample  Solu?on  (cont.)  

32  

Page 33: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Sample  Solu?on  (cont.)  

33  

Page 34: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Tes?ng  Your  Solu?on  

•  Solu?on  has  lots  of  comparisons  •  Set  up  various  Shoe  objects  •  Compare  them:  – Write  down  expected  output    (based  on  your  own  logic  in  the  comments)  

– Display  expected  output  via  println() – Are  they  the  same?  

•  Make  sure  to  check  all  your  comparisons  

34  

Page 35: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Tes?ng  the  Sample  Solu?on  

35  

Page 36: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Interface  Hierarchies  •  An  interface  can  inherit  from  another  interface  •  Example:  

 

•  A  class  that  implements  Volleyball  needs  to  define  how  many  methods?  

36  

public interface Sports { public void setHomeTeam( String name ); public void setVisitingTeam( String name ); } public interface Volleyball extends Sports { public void setHomeTeamScore( int points ); public void setVisitingTeamScore( int points ); }

Page 37: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Interface  Hierarchies  

•  A  child  interface  inherits  all  abstract  methods  from  the  parent  

•  A  class  implemen?ng  the  child  interface  must  therefore  define  all  methods  from  both  interfaces  

•  Class  hierarchies  and  interface  hierarchies  don’t  overlap  – A  class  cannot  extends  an  interface  – A  class  can  only  implements  an  interface  

37  

Page 38: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Mul?ple  Interfaces  

•  A  class  can  implement  mul?ple  interfaces  

 •  All  listed  in  the  same  implements  clause  

38  

public class ManyThings implements interface1, interface2 { // all methods of both interfaces }

Page 39: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Simple  Example  

39  

Page 40: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Simple  Example  

40  

Page 41: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Simple  Example  

41  

Page 42: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Combining  extends  and  implements

•  Recall  origin  of  interfaces:  a  way  to  get  around  not  having  mul?ple  inheritance  

•  A  class  can  extend  another  class  and  implement  yet  other  classes  

•  Example:  PickupTruck  inherits  from  Truck  PickupTruck  implements  Car

42  

Page 43: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

PickupTruck  Example  

43  

Page 44: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Summary  of  Interface  Concepts  •  Interface  makes  classes  conform  to  a  communica?on  standard  –  All  methods  are  abstract  –  Can  include  constants  defini?ons  –  Cannot  be  instan?ated  –  No  a\ributes  

•  New  reserved  word:  implements •  Interfaces  can  extend  other  interfaces  to  form  an  interface  hierarchy  –  All  abstract  methods  are  inherited  

•  Overcomes  not  having  mul?ple  inheritance  •  Comparable  interface  comes  with  compareTo()

44  

Page 45: COSC121:$$ Computer$Programming$IIpublic abstract class Animal { protected int numLegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs()

Admin  

•  Next  class:  – Prac?ce  between  inheritance  and  interfaces  – Use  them  (both)  in  polymorphism  aper  that  

•  Also  use  arrays  and  loops  –  finish  A1  Q1  by  Thursday    

•  Labs  next  week:  – Prac?ce  inheritance  

45