6.#Class,#OOP# August18,#2015#

19
6. Class, OOP August 18, 2015 Minhaeng Lee IEDU Programming AcBvity, Summer, 2015

Transcript of 6.#Class,#OOP# August18,#2015#

Page 1: 6.#Class,#OOP# August18,#2015#

6.  Class,  OOP  August  18,  2015  

   

Minhaeng  Lee  

IEDU  Programming  AcBvity,  Summer,  2015  

Page 2: 6.#Class,#OOP# August18,#2015#

FOP  and  OOP  •   FuncBon  Oriented  Programming  

–   FuncBon  call  from  main  –   Main  –  funcBon  –  main  –  funcBon  …  –   Difficult  to  management  –   To  replace  funcBon,  we  need  to  edit  file  itself  –   Pure  C  style.  Nowadays,  not  popular.  

•   Object  Oriented  Programming  –   Object  call  from  main  –   Main  –  object  –  method  –  object  –  main  …  –   Easier  to  management  –   To  replace  class,  we  need  to  change  file  (class)  –   SBll  popular  in  Java,  C++,  python,  javascript,  ….  

Page 3: 6.#Class,#OOP# August18,#2015#

FuncBon  Oriented  Programming  

return  

main  

funcBon  

funcBon  

returns  

calls  

1   2  

3   4  

Class  

Class  concept  doesn’t  have  any  big  

meaning  

Page 4: 6.#Class,#OOP# August18,#2015#

Object  Oriented  Programming  

main  

funcBon  

returns  

calls  

1   2  

3   4  

Class  A  

return  

Public  FuncBon  

Private  funcBon  

Private  funcBon  

returns  

calls  

1   2  

3   4  

Class  B  

Instance  of  Class  B  

Interface  

Page 5: 6.#Class,#OOP# August18,#2015#

Class  •   Consist  of    

–   Variables  •   Variables  declared  in  class  (but  outside  of  method)  

–   Methods  •   Similar  meaning  with  funcBon.  We  use  both  

–   Constructor  •   A  Method  called  when  a  class  is  iniBalized  (=allocated  to  memory  by  new  keyword)  

•   Instance?  –   The  class  variable  made  by  new  keyword  –   Memory  allocated  class  variable  –   Example  

•   a  is  an  instance  •   MyClass  a  =  new  MyClass();  

•   Inheritance  (today)    •   Polymorphism  (next  week)  

Page 6: 6.#Class,#OOP# August18,#2015#

Constructor  

•   A  method  called  at  memory  allocaBon  •   IniBalizaBon  process  involved  –  Has  class  name  as  its  name  –  Similar  to  funcBon  but  without  return  type  –  public  <className>  (<parameters>)  {  }  

•   Default  constructor  is  defined  already  –  public  <className>()  {  }  

Page 7: 6.#Class,#OOP# August18,#2015#

Circle.java  

ClassTest.java  

Page 8: 6.#Class,#OOP# August18,#2015#

Access  Specifier  •   Public  –   Allow  to  everybody  –   Outside  of  class  to  local  variables/methods  

•   private  (default)  –   Allow  to  class  itself  –   Inside  of  class  to  its  local  variables/methods  

•   Protected  –   Allow  to  its  descent  –   Inside  of  descent  (children  inherit  itself)  to  class  itself  

Page 9: 6.#Class,#OOP# August18,#2015#

extends  keyword  for  inheritance    

hdp://www.tutorialspoint.com/java/java_inheritance.htm  

•   Sub  has  all  properBes  (variables  and  methods)  of  Super  

•   When?  •   Divide  file  (or  class)  based  

on  their  role  •   Make  common  variables  or  

methods  of  Sub  into  Super  

Page 10: 6.#Class,#OOP# August18,#2015#

extends  keyword  for  inheritance    

hdp://www.tutorialspoint.com/java/java_inheritance.htm  

Page 11: 6.#Class,#OOP# August18,#2015#

extends  keyword  for  inheritance    

hdp://www.tutorialspoint.com/java/java_inheritance.htm  

Page 12: 6.#Class,#OOP# August18,#2015#

Senior  Programmer  Team  Members  

                           

PracBce  :  Programmer  Company  

Programmer  Computer  ID  

                 

Employee    

Name  Age  

Page 13: 6.#Class,#OOP# August18,#2015#

Employee.java  

Page 14: 6.#Class,#OOP# August18,#2015#

Programmer.java  

Page 15: 6.#Class,#OOP# August18,#2015#

SeniorProgrammer.java  

Page 16: 6.#Class,#OOP# August18,#2015#

Company.java  (to  contain  main)  

Page 17: 6.#Class,#OOP# August18,#2015#

Data  Structure  :  preview  

•  Stack  •  Linked  list  •  Tree  •  HashMap  

Page 18: 6.#Class,#OOP# August18,#2015#

QuesBons  

Page 19: 6.#Class,#OOP# August18,#2015#

References  

•   IBM  –  hdp://www.ibm.com/developerworks/java/tutorials/j-­‐introtojava1/j-­‐introtojava1-­‐pdf.pdf