Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Object Oriented Programming with Java

14,839 views

Published on

Published in: Technology, Sports
  • Follow the link, new dating source: ❶❶❶ http://bit.ly/36cXjBY ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating for everyone is here: ❶❶❶ http://bit.ly/36cXjBY ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Super awesome course!! We were able to easily understand and apply the instructions with our fur baby Sola. ◆◆◆ https://bit.ly/38b79Wm
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Go here now to unlock your dog's natural intelligence today.  http://t.cn/Aie4mTQb
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Object Oriented Programming with Java

  1. 1. Object Orientated Programming with Java Jussi Pohjolainen Tampere University of Applied Sciences
  2. 2. MOTIVATION
  3. 3. Job Opportunities? <ul><li>Little acronyms from job descriptions: </li></ul><ul><ul><li>S60, C++, J2ME, Java, Java EE, SCRUM, JAX-WS 2.0, EJB 3.0, Spring, Hybernate, Struts, SQL, XHTML, CSS, Symbian C++, Perl, PHP, Python, LDAP, MFC, XML, .NET, Visual Basic, AJAX, Objective-C, JSP, Servlet, GTK, Qt, JavaScript, Oracle, SQL Server, DB Design… </li></ul></ul><ul><li>Where is Object Orientated programming? </li></ul>
  4. 4. Developing S60 apps? “ Basic” programming skills OO skills Data Structures / DB UI programming Symbian C++ S60
  5. 5. Developing iPhone apps? “ Basic” programming skills OO skills Data Structures / DB UI programming Objective-C iPhone
  6. 6. Web-development with Java? “ Basic” programming skills OO skills Data Structures /DB UI programming Java Servlet Www-techniques: Xhtml, CSS, XML Applets JSP
  7. 7. OO CONCEPTS
  8. 8. Intro to OO <ul><li>Object Orientated programming is a programming paradigm. </li></ul><ul><li>One way of describing the structure of the application </li></ul><ul><li>Several paradigms available. </li></ul><ul><ul><li>Procedural, Functional, ... </li></ul></ul><ul><li>OO is nowdays one of the most popular. </li></ul><ul><ul><li>C++ </li></ul></ul><ul><ul><li>Java </li></ul></ul><ul><ul><li>C# </li></ul></ul><ul><ul><li>PHP 5 </li></ul></ul><ul><ul><li>... </li></ul></ul>
  9. 9. Benefits <ul><li>Reusability </li></ul><ul><ul><li>Once you written code, you can reuse it! </li></ul></ul><ul><li>Management </li></ul><ul><ul><li>Application is programmed using classes and objects. Pieces that communicate with each other. </li></ul></ul><ul><li>Maintanence </li></ul><ul><ul><li>When changing the code, it does not influence the whole application. </li></ul></ul>
  10. 10. Basic Concept: Object <ul><li>In real life, the world consists of objects: </li></ul><ul><ul><li>cars, buildings, trees, ships, humans, flowers.. </li></ul></ul><ul><li>Every object has actions (=methods!) that can incluence other objects </li></ul><ul><ul><li>jack drives ferrari. </li></ul></ul><ul><ul><li>Object jack has a method drive that influences some way to object ferrari. </li></ul></ul><ul><ul><li>jack.drive(ferrari); </li></ul></ul><ul><li>In OO, you should implement the app so that it consists of objects that influence each other! </li></ul>
  11. 11. Example about an Object <ul><li>Datsun 100A is an object </li></ul><ul><li>Datsun 100A has different actions or methods : drive, brake, park... </li></ul><ul><li>Datsun 100A has information or attributes : color, amount of gears, amount of doors... </li></ul>
  12. 12. Basic Concept: Class <ul><li>Class is a blueprint or template of an object </li></ul><ul><li>Class describes the state and behaviour to it's objects. </li></ul><ul><li>Object is created from the class. </li></ul>
  13. 13. Example about an Class <ul><li>If Datsun 100A is an object, Car is a class. </li></ul><ul><li>If one wants to create Datsun 100A, you have to have first the blueprints of the Datsun. </li></ul><ul><li>Blueprints of an object: Class </li></ul><ul><li>Class Car -> Object Datsun 100A </li></ul>
  14. 14. Examples: Class to Object Class Object Car datsun 100a Human Jack Bauer Color red Laptop MacBook Pro String &quot;some string&quot; Array {1,2,3,2,4} ... ...
  15. 15. Class and Object <ul><li>Car - class </li></ul>Datsun 100A Lamborghini Diablo Peugeot 406
  16. 16. Car's Blueprint <ul><li>When building a Car's blueprint (class), you have to think that what is similar in all car's </li></ul><ul><li>So what is similar in datsun, lamborghini and peugeot? </li></ul>
  17. 17. Objects datsun, lambo, peugeot <ul><li>datsun: </li></ul><ul><ul><li>brand: Datsun 100A , motor: 1.0, fuzzy dices: yes, color: red </li></ul></ul><ul><li>lambo </li></ul><ul><ul><li>brand: Lamborghini Diablo, motor: 8.0, fuzzy dices: no, color: punainen </li></ul></ul><ul><li>peugeot </li></ul><ul><ul><li>brand: Peugeot 406, motor: 2.2, fuzzy dices: no, color: blue </li></ul></ul>
  18. 18. Car's Blueprint (Class) in UML Car brand motor amountOfDoors color hasFuzzyDices . .
  19. 19. From Class to Object datsun Datsun 100A 1.0 3 red true lambo Lamborghini Diablo 8.0 3 red false Car brand motor amountOfDoors color hasFuzzyDices . .
  20. 20. Car-class, extension Car brand motor amountOfDoors color hasFuzzyDices drive park brake
  21. 21. Class <ul><li>Class is a template or blueprint to object </li></ul><ul><li>Class holds </li></ul><ul><ul><li>Attributes (=variables) </li></ul></ul><ul><ul><li>Actions (=methods) </li></ul></ul><ul><li>Class instances are called objects </li></ul>
  22. 22. CLASSES AND OBJECTS IN JAVA
  23. 23. Person Class to Objects george George Smith 40 Teacher 09-12345 Jack Jack Puupää 60 Toimistopäällikkö 03-654321 eat sleep drinkBeer Person firstname lastname age profession phonenumber eat sleep drinkBeer
  24. 24. Person – class to Java <ul><li>class Person { </li></ul><ul><li>public String firstname; </li></ul><ul><li> public String lastname; </li></ul><ul><li> public int age; </li></ul><ul><li> public String profession; </li></ul><ul><li> public int phonenumber; </li></ul><ul><li> public void eat() { </li></ul><ul><li>System.out.println(&quot;Eating!&quot;); </li></ul><ul><li>} </li></ul><ul><li> public void sleep() { </li></ul><ul><li>System.out.println(&quot;Sleeping!&quot;); </li></ul><ul><li>} </li></ul><ul><li>public void drinkBeer() { </li></ul><ul><li>System.out.println(&quot;Drinking!&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Person firstname lastname age profession phonenumber eat sleep drinkBeer
  25. 25. From Class to Object <ul><li>App always starts from the main-method </li></ul><ul><li>Let's test the Person – class </li></ul><ul><li>This creates a variable a which type is integer </li></ul><ul><ul><li>int a; </li></ul></ul><ul><li>This creates a object jack which type is Person </li></ul><ul><ul><li>Person jack; </li></ul></ul>
  26. 26. From Class to Object <ul><li>class Person { </li></ul><ul><li>.... </li></ul><ul><li>} </li></ul><ul><li>class JustTesting { </li></ul><ul><li>public static void main(String [] args) </li></ul><ul><li>{ </li></ul><ul><li>// Declare the object </li></ul><ul><li>Person jack; </li></ul><ul><li>// Initialize the object </li></ul><ul><li>jack = new Person(); </li></ul><ul><li>jack.firstname = &quot;Jack&quot;; </li></ul><ul><li>jack.lastname = &quot;Smith&quot;; </li></ul><ul><li>jack.drinkBeer(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  27. 27. Example: Car - class <ul><li>class Car </li></ul><ul><li>{ </li></ul><ul><li>public String brand; </li></ul><ul><li>public int amountOfGas; </li></ul><ul><li>public void drive() { </li></ul><ul><li>amountOfGas--; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  28. 28. Creating Objects From the Class <ul><li>class Car { </li></ul><ul><li>.... </li></ul><ul><li>} </li></ul><ul><li>class JustTesting { </li></ul><ul><li>public static void main(String [] args) </li></ul><ul><li>{ </li></ul><ul><li>Car datsun = new Car(); </li></ul><ul><li>datsun.amountOfGas = 100; </li></ul><ul><li>datsun.drive(); </li></ul><ul><li>System.out.println(datsun.amountOfGas); </li></ul><ul><li>Car ferrari = new Car(); </li></ul><ul><li>ferrari.amountOfGas = 300; </li></ul><ul><li>ferrari.drive(); </li></ul><ul><li>System.out.println(ferrari.amountOfGas); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  29. 29. Basic Concept - Encapsulation private public method
  30. 30. About Attributes <ul><li>Attributes are usually marked as private </li></ul><ul><li>The reason for this is that other objects cannot change the values as they will </li></ul><ul><li>You don't for example want that every object in the world can change person's weight to 500kg... </li></ul>
  31. 31. Example: Person - class <ul><li>class Person { </li></ul><ul><li>private String name; </li></ul><ul><li>private int weight; </li></ul><ul><li>} </li></ul>
  32. 32. <ul><li>class Person { </li></ul><ul><li>private String name; </li></ul><ul><li>private int weight; </li></ul><ul><li>} </li></ul><ul><li>class JustTesting { </li></ul><ul><li>public static void main(String [] args) </li></ul><ul><li>{ </li></ul><ul><li>Person jack = new Person(); </li></ul><ul><li>jack.name = &quot;Jack Smith&quot;; </li></ul><ul><li>jack.weight = 500; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>RESULT: </li></ul><ul><li>TB308POHJUS-L-2:temp pohjus$ javac Person.java </li></ul><ul><li>Person.java:9: name has private access in Person </li></ul><ul><li>jack.name = &quot;Jack Smith&quot;; </li></ul><ul><li>^ </li></ul><ul><li>Person.java:10: weight has private access in Person </li></ul><ul><li>jack.weight = 500; </li></ul><ul><li>^ </li></ul><ul><li>2 errors </li></ul>
  33. 33. <ul><li>class Person { </li></ul><ul><li>private String name; </li></ul><ul><li>private int weight; </li></ul><ul><li>public void setName(String n) { </li></ul><ul><li>name = n; </li></ul><ul><li>} </li></ul><ul><li>public String getName() { </li></ul><ul><li>return name; </li></ul><ul><li>} </li></ul><ul><li>public void setWeight(int w) { </li></ul><ul><li>if(w > 0 && w <= 150) </li></ul><ul><li>weight = w; </li></ul><ul><li>} </li></ul><ul><li>public int getWeight() { </li></ul><ul><li>return weight; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class JustTesting { </li></ul><ul><li>public static void main(String [] args) </li></ul><ul><li>{ </li></ul><ul><li>Person jack = new Person(); </li></ul><ul><li>jack.setName(&quot;Jack Smith&quot;); </li></ul><ul><li>jack.setWeight(200); </li></ul><ul><li>System.out.println(jack.getName()); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  34. 34. Accessor and Mutator - methods <ul><li>class Person { </li></ul><ul><li>private String name; </li></ul><ul><li>private int weight; </li></ul><ul><li>// Mutator </li></ul><ul><li>public void setName(String n) { </li></ul><ul><li>name = n; </li></ul><ul><li>} </li></ul><ul><li>// Accessor </li></ul><ul><li>public String getName() { </li></ul><ul><li>return name; </li></ul><ul><li>} </li></ul><ul><li>// Mutator </li></ul><ul><li>public void setWeight(int w) { </li></ul><ul><li>if(w > 0 && w <= 150) </li></ul><ul><li>weight = w; </li></ul><ul><li>} </li></ul><ul><li>// Accessor </li></ul><ul><li>public int getWeight() { </li></ul><ul><li>return weight; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  35. 35. JAVA TYPES
  36. 36. Java Types <ul><li>Java has two type of types </li></ul><ul><li>1) Primitive types </li></ul><ul><ul><li>byte, short, int, long, double, float, char, boolean </li></ul></ul><ul><li>2) Class types </li></ul><ul><ul><li>String, Scanner, Array, JButton, JFrame ... </li></ul></ul>
  37. 37. Differences <ul><li>Primitive types are spelled with lowercase: </li></ul><ul><ul><li>int, double, float... </li></ul></ul><ul><li>Class types are spelled with uppercase </li></ul><ul><ul><li>String, Scanner, Person, Cat, Car ... </li></ul></ul><ul><li>Primitive type declaring and initialization </li></ul><ul><ul><li>int a = 5; </li></ul></ul><ul><li>Class type declaring and initialization with new </li></ul><ul><ul><li>Dog spot = new Dog(); </li></ul></ul>
  38. 38. Differences <ul><li>Primitive type </li></ul><ul><ul><li>int a = 5; </li></ul></ul><ul><li>Class type </li></ul><ul><ul><li>int [] b= new int[5]; </li></ul></ul><ul><li>b holds memory address </li></ul><ul><li>a holds value 5. </li></ul>
  39. 39. Memory Address? <ul><li>int [] b = new int[2]; </li></ul><ul><li>b[0] = 1; </li></ul><ul><li>b[1] = 2; </li></ul><ul><li>// prints 0x01 </li></ul><ul><li>System.out.println(b); </li></ul>RAM variable b address value 0x01 1 0x02 2 address value 0x09 0x01
  40. 40. Memory Address? <ul><li>int [] b = new int[2]; </li></ul><ul><li>b[0] = 1; </li></ul><ul><li>b[1] = 2; </li></ul><ul><li>int [] a = b; </li></ul><ul><li>// prints 0x01 </li></ul><ul><li>System.out.println(b); </li></ul><ul><li>// prints 0x01 </li></ul><ul><li>System.out.println(a); </li></ul>RAM variable b variable a address value 0x01 1 0x02 2 address value 0x09 0x01 address value 0x19 0x01
  41. 41. Output? <ul><li>int [] b = new int[2]; </li></ul><ul><li>b[0] = 1; </li></ul><ul><li>b[1] = 2; </li></ul><ul><li>int [] a = b; </li></ul><ul><li>b[0] = 99; </li></ul><ul><li>// Output? </li></ul><ul><li>System.out.println(a[0]); </li></ul>
  42. 42. Differences Again <ul><li>Primitive type </li></ul><ul><ul><li>int a = 5; </li></ul></ul><ul><li>Class type </li></ul><ul><ul><li>int [] b= new int[5]; </li></ul></ul><ul><li>b holds memory address </li></ul><ul><li>a holds value 5. </li></ul>
  43. 43. Differences Again <ul><li>Primitive type </li></ul><ul><ul><li>int a = 5; </li></ul></ul><ul><li>Class type </li></ul><ul><ul><li>Person jack = new Person() </li></ul></ul><ul><li>jack holds memory address </li></ul><ul><li>a holds value 5. </li></ul>
  44. 44. Output? <ul><li>Person jack = new Person(); </li></ul><ul><li>jack.setName(&quot;Jack Smith&quot;); </li></ul><ul><li>Person james = jack; </li></ul><ul><li>james.setName(&quot;James Bond&quot;); </li></ul><ul><li>// output? </li></ul><ul><li>System.out.println(jack.getName()); </li></ul>
  45. 45. Methods and Variables <ul><li>public void method(int x) { </li></ul><ul><li>x++; </li></ul><ul><li>} </li></ul><ul><li>public void main(String [] args) { </li></ul><ul><li>int y = 3; </li></ul><ul><li>method(y); </li></ul><ul><li>// Output is 3! </li></ul><ul><li>System.out.println(y); </li></ul><ul><li>} </li></ul>
  46. 46. Methods and Variables <ul><li>public void method(int [] x) { </li></ul><ul><li>x[0] = 12; </li></ul><ul><li>} </li></ul><ul><li>public void main(String [] args) { </li></ul><ul><li>int [] y = {1,2,3}; </li></ul><ul><li>method(y); </li></ul><ul><li>// Output is 12 since array is class type! </li></ul><ul><li>System.out.println(y[0]); </li></ul><ul><li>} </li></ul>
  47. 47. String <ul><li>String is an exception to the rules </li></ul><ul><li>String is a class type that acts like primitive type </li></ul><ul><li>String is the only class type that can be initialized without the new word. </li></ul><ul><ul><li>String a = &quot;hello&quot;; </li></ul></ul><ul><li>String is passed by value in methods, so String is copied when moving strings in methods. </li></ul>
  48. 48. String and Memory <ul><li>String variables are objects => holds memory address. </li></ul><ul><li>Comparing contents </li></ul><ul><ul><li>a.equals(b); </li></ul></ul><ul><li>Comparing memory addresses </li></ul><ul><ul><li>a == b </li></ul></ul>
  49. 49. CONSTRUCTOR
  50. 50. Constructors <ul><li>Constructor is a “init method” that is called when an object is created </li></ul><ul><li>Java provides default constructor (= constructor with no parameters) </li></ul><ul><li>Constructor has the same name than the class </li></ul><ul><li>Constructor does not return anything </li></ul><ul><li>Constructor usually initalizes class members </li></ul>
  51. 51. Example <ul><li>class Car { </li></ul><ul><li>public Car() { </li></ul><ul><li>System.out.println(&quot;Constructor!&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Car datsun = new Car(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>> java Test Constructor!
  52. 52. <ul><li>class Car { </li></ul><ul><li>private String brand; </li></ul><ul><li>public Car(String b) { </li></ul><ul><li>brand = b; </li></ul><ul><li>} </li></ul><ul><li>public String getBrand() { </li></ul><ul><li>return brand; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Car datsun = new Car(&quot;Datsun 100A&quot;); </li></ul><ul><li>System.out.println( datsun.getBrand() ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>> java Test Datsun 100A
  53. 53. Multiple Constructors <ul><li>class Car { </li></ul><ul><li>public Car() { </li></ul><ul><li>// Do something </li></ul><ul><li>} </li></ul><ul><li>public Car(String brand) { </li></ul><ul><li>// Do something else </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Car datsun = new Car(); </li></ul><ul><li>Car ferrari = new Car(&quot;Ferrari&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  54. 54. Problem? <ul><li>class Car { </li></ul><ul><li>String brand; </li></ul><ul><li>public Car(String brand) { </li></ul><ul><li>brand = brand; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Car datsun = new Car(&quot;Datsun 100a&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>> java Test null
  55. 55. Solution <ul><li>class Car { </li></ul><ul><li>String brand; </li></ul><ul><li>public Car(String brand) { </li></ul><ul><li>this.brand = brand; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Car datsun = new Car(&quot;Datsun 100a&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>> java Test Datsun 100a
  56. 56. COMPOSITION
  57. 57. Composition <ul><li>Relatioship between objects, where one object owns, or has the other object </li></ul><ul><li>Car has or owns Motor </li></ul><ul><li>When Car is build, it's motor is built also </li></ul><ul><li>When Car is destroyed it's motor is destroyed </li></ul>
  58. 58. UML notation
  59. 59. Java: Composition <ul><li>// Composition </li></ul><ul><li>class Car </li></ul><ul><li>{ </li></ul><ul><li>private Motor motor; </li></ul><ul><li>public Car() { </li></ul><ul><li>motor = new Motor(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  60. 60. One to Many?
  61. 61. Java: One to Many <ul><li>class Department </li></ul><ul><li>{ </li></ul><ul><li>private Professor [] members; </li></ul><ul><li>private int numberOfMembers; </li></ul><ul><li>public Department(Professor prof) { </li></ul><ul><li>members = new Professor[20]; </li></ul><ul><li>members[0] = prof; </li></ul><ul><li>numberOfMembers = 1; </li></ul><ul><li>} </li></ul><ul><li>public void addProfessor(Professor prof) { </li></ul><ul><li>members[numberOfMembers] = prof; </li></ul><ul><li>numberOfMembers++; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  62. 62. INHERITANCE
  63. 63. Introduction to Inheritance <ul><li>Inheritance is a relationship between two or more classes where derived class inherites behaviour and attributes of pre-existing (base) classes </li></ul><ul><li>Intended to help reuse of existing code with little or no modification </li></ul>
  64. 64. Inheritance <ul><li>Inheritance can be continous </li></ul><ul><ul><li>Derived class can inherit another class, which inherits another class and so on </li></ul></ul><ul><ul><li>When changing the base class all the derived classes changes also </li></ul></ul><ul><li>Example: </li></ul><ul><ul><li>Mammal <– Human <– Worker <- Programmer </li></ul></ul><ul><li>Could mammal be a derived class? If so, what would be the base class? </li></ul>
  65. 65. Picture about Inheritance C lass B F eatures: a,b,c C lass D F eatures: a,b,d,e,f a b C lass A features: a,b c d e C lass C F eatures: a,b,d,e f
  66. 66. Multiple Inheritance <ul><li>In multiple inheritance a derived class has multiple base classes </li></ul><ul><li>C++ supports multiple base classes, Java don't </li></ul>Driver - license - Y ear of approval Conductor - A ccount number Taxi Driver - area House Boat Houseboat
  67. 67. Inheritance and Capsulation <ul><li>private </li></ul><ul><ul><li>Is accessible only via the base class </li></ul></ul><ul><li>public </li></ul><ul><ul><li>Is accessible everywhere (base class, derived class, othe classes) </li></ul></ul><ul><li>protected </li></ul><ul><ul><li>Is accessible by the base class and derived classes </li></ul></ul>
  68. 68. Basic example <ul><li>What are Programmer's attributes and methods? </li></ul>Human string name void sleep() void drink() void eat() Programmer int salary void implementApps() void beNerd()
  69. 69. Overriding? <ul><li>What about now? </li></ul>Human string name void sleep() void drink() void eat() Programmer int salary void implementApps() void beNerd() void drink() void eat()
  70. 70. Overriding <ul><li>Since programmer eats and drinks differently than humans (only Coke and Pizza) the eat and drink methods are overriden in Programmer! </li></ul>
  71. 71. Abstract Class <ul><li>Abstract class is a class which you cannot instantiate (create objects) </li></ul><ul><li>You can inherit abstract class and create objects from the inherited class, if it is concrete one </li></ul><ul><li>Abstract class in C++ has abstract methods, that do not have implementations </li></ul><ul><li>These methods forces derived classes to implement those methods </li></ul>
  72. 72. Example <<abstract>> Mammal string name void makesound() {abstract} Elephant int trunkLength makesound()
  73. 73. Example <<abstract>> Figure int x, y double calculateArea() {abstract} Circle double radius double calculateArea() Rect double length, height double calculateArea()
  74. 74. INHERITANCE IN JAVA
  75. 75. Example: Basic Inheritance <ul><li>class Human { </li></ul><ul><li>public void sleep() { </li></ul><ul><li>System.out.println(&quot;Human sleeps&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Programmer extends Human { </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Programmer jussi = new Programmer(); </li></ul><ul><li>jussi.sleep(); // &quot;Human sleeps&quot; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  76. 76. Example: Overriding <ul><li>class Human { </li></ul><ul><li>public void sleep() { </li></ul><ul><li>System.out.println(&quot;Human sleeps&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Programmer extends Human { </li></ul><ul><li>public void sleep() { </li></ul><ul><li>System.out.println(&quot;Programmer sleeps&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Programmer jussi = new Programmer(); </li></ul><ul><li>jussi.sleep(); // &quot;Programmer sleeps&quot; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  77. 77. Example: super <ul><li>class Human { </li></ul><ul><li>public void sleep() { </li></ul><ul><li>System.out.println(&quot;Human sleeps&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Programmer extends Human { </li></ul><ul><li>public void sleep() { </li></ul><ul><li>super.sleep(); </li></ul><ul><li>System.out.println(&quot;Programmer sleeps&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Programmer jussi = new Programmer(); </li></ul><ul><li>jussi.sleep(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>> java Test Human sleeps Programmer sleeps
  78. 78. Constructors and Inheritance <ul><li>class Human { </li></ul><ul><li>public Human() { </li></ul><ul><li>System.out.println(&quot;Human&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Programmer extends Human { </li></ul><ul><li>public Programmer() { </li></ul><ul><li>System.out.println(&quot;Programmer&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Programmer jussi = new Programmer(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>> java Test Human Programmer
  79. 79. Constructors and Inheritance <ul><li>Constructor allways calls the base classes constructor! </li></ul><ul><li>When creating a constructor </li></ul><ul><ul><li>void Human() { </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Java adds super() – call to it: </li></ul><ul><ul><li>void Human() { </li></ul></ul><ul><ul><li>super(); // calls base classes constructor </li></ul></ul><ul><ul><li>} </li></ul></ul>
  80. 80. <ul><li>class Human { </li></ul><ul><li>public Human() { </li></ul><ul><li>System.out.println(&quot;Human&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Programmer extends Human { </li></ul><ul><li>public Programmer() { </li></ul><ul><li>System.out.println(&quot;Programmer&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Programmer jussi = new Programmer(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>> java Test Human Programmer
  81. 81. <ul><li>class Human { </li></ul><ul><li>public Human() { </li></ul><ul><li>super(); // Java adds this! Calls base classes contructor </li></ul><ul><li>System.out.println(&quot;Human&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Programmer extends Human { </li></ul><ul><li>public Programmer() { </li></ul><ul><li>super(); // Java adds this! Calls base classes contructor </li></ul><ul><li>System.out.println(&quot;Programmer&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Programmer jussi = new Programmer(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>> java Test Human Programmer
  82. 82. What the...? What base class? <ul><li>class Human { </li></ul><ul><li>public Human() { </li></ul><ul><li>// Java adds this! Calls base classes contructor </li></ul><ul><li>super(); </li></ul><ul><li>System.out.println(&quot;Human&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  83. 83. Object <ul><li>Every class derives from a class called Object. </li></ul><ul><li>// Java adds the extends Object too! </li></ul><ul><li>class Human extends Object { </li></ul><ul><li>public Human() { </li></ul><ul><li>super(); </li></ul><ul><li>System.out.println(&quot;Human&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  84. 84. Object clone() equals() finalize() toString() ... Human String name ... http://java.sun.com/javase/6/docs/api/java/lang/Object.html
  85. 85. <ul><li>class Human { </li></ul><ul><li>public Human( int a ) { </li></ul><ul><li>System.out.println(&quot;Human&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Programmer extends Human { </li></ul><ul><li>public Programmer() { </li></ul><ul><li>System.out.println(&quot;Programmer&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Programmer jussi = new Programmer(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>> javac Test.java > DOES NOT COMPILE!!! Why?
  86. 86. <ul><li>class Human { </li></ul><ul><li>public Human( int a ) { </li></ul><ul><li>System.out.println(&quot;Human&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Programmer extends Human { </li></ul><ul><li>public Programmer() { </li></ul><ul><li>super(); // Java adds this and it calls constructor </li></ul><ul><li>// Human() that does not exist.. </li></ul><ul><li>System.out.println(&quot;Programmer&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Programmer jussi = new Programmer(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  87. 87. <ul><li>class Human { </li></ul><ul><li>public Human( int a ) { </li></ul><ul><li>System.out.println(&quot;Human&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Programmer extends Human { </li></ul><ul><li>public Programmer() { </li></ul><ul><li>super(5); // Now it works: Human(int a) exists. </li></ul><ul><li>System.out.println(&quot;Programmer&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Programmer jussi = new Programmer(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  88. 88. <ul><li>class Human { </li></ul><ul><li>private String name </li></ul><ul><li>public Human( String name ) { </li></ul><ul><li>this.name = name; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Programmer extends Human { </li></ul><ul><li>private int salary; </li></ul><ul><li>public Programmer(String name, int salary) { </li></ul><ul><li>super(name); </li></ul><ul><li>this.salary = salary; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Test { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Programmer jussi = new Programmer(&quot;Jussi&quot;, 5000); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  89. 89. Abstract Class <ul><li>From abstract class you cannot create objects! </li></ul><ul><li>Abstract class is usually used with inheritance </li></ul><ul><li>Abstract class may contain abstract methods. </li></ul><ul><li>Abstract method forces derived classes to implement the abstract method. </li></ul>
  90. 90. Abstract Class: example <ul><li>abstract class Mammal { </li></ul><ul><li>abstract void makeSound(); </li></ul><ul><li>} </li></ul><ul><li>class Dog extends Mammal { </li></ul><ul><li>// You have to implement this! </li></ul><ul><li>public void makeSound() { </li></ul><ul><li>System.out.println(&quot;Bark!&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  91. 91. Abstract Class: example <ul><li>// Does NOT work, since Mammal is </li></ul><ul><li>// abstract class </li></ul><ul><li>Mammal object = new Mammal(); </li></ul><ul><li>// Does work </li></ul><ul><li>Dog spot = new Dog(); </li></ul>
  92. 92. Java: Abstract class and Interface <ul><li>Abstract class can hold &quot;normal&quot; methods and abstract methods. </li></ul><ul><li>Interface holds only abstract methods </li></ul><ul><li>Abstract class: </li></ul><ul><ul><li>class A extends someAbstractClass </li></ul></ul><ul><li>Interface </li></ul><ul><ul><li>class A implements someInterface </li></ul></ul>
  93. 93. Abstract class to Interface <ul><li>abstract class Movable { </li></ul><ul><li>abstract public void start(); </li></ul><ul><li>abstract public void stop(); </li></ul><ul><li>} </li></ul><ul><li> </li></ul><ul><li>interface Movable { </li></ul><ul><li>public void start(); </li></ul><ul><li>public void stop(); </li></ul><ul><li>} </li></ul>
  94. 94. Implementing the Interface <ul><li>interface Movable { </li></ul><ul><li>public void start(); </li></ul><ul><li>public void stop(); </li></ul><ul><li>} </li></ul><ul><li>class Car implements Movable { </li></ul><ul><li>// You have to implement these </li></ul><ul><li>public void start() { </li></ul><ul><li>// Do something </li></ul><ul><li>} </li></ul><ul><li>public void stop() { </li></ul><ul><li>// Do something </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  95. 95. Abstract class vs Interface <ul><li>Abstract class can hold normal methods and abstract methods </li></ul><ul><li>Interface can hold only abstract methods </li></ul><ul><li>Class can inherite only one base class </li></ul><ul><li>Class can implement several interfaces! </li></ul>
  96. 96. <ul><li>class Car extends Vehicle implements Movable, RunsOnGasoline { </li></ul><ul><li>// You have to implement these </li></ul><ul><li>public void start() { </li></ul><ul><li>// Do something </li></ul><ul><li>} </li></ul><ul><li>public void stop() { </li></ul><ul><li>// Do something </li></ul><ul><li>} </li></ul><ul><li>public void reduceGasoline() { </li></ul><ul><li>// Do something </li></ul><ul><li>} </li></ul><ul><li>public void addGasoline() { </li></ul><ul><li>// Do something </li></ul><ul><li>  } </li></ul><ul><li>} </li></ul>
  97. 97. POLYMORPHISM
  98. 98. int as parameter <ul><li>class Exercise13 { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>int x = 4; </li></ul><ul><li>myMethod(x); </li></ul><ul><li>} </li></ul><ul><li>public static void myMethod(int a) { </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  99. 99. Human parameter <ul><li>class Human { </li></ul><ul><li>} </li></ul><ul><li>class Exercise13 { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Human jack = new Human(); </li></ul><ul><li>myMethod(jack); </li></ul><ul><li>} </li></ul><ul><li>public static void myMethod(Human a) { </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  100. 100. Mammal parameter <ul><li>class Mammal { </li></ul><ul><li>} </li></ul><ul><li>class Human extends Mammal { </li></ul><ul><li>} </li></ul><ul><li>class Dog extends Mammal { </li></ul><ul><li>} </li></ul><ul><li>class Exercise13 { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Human jack = new Human(); </li></ul><ul><li>Dog spot = new Dog(); </li></ul><ul><li>Mammal mammal = new Mammal(); </li></ul><ul><li>// these work! You can pass mammals, dogs and humans to the method! </li></ul><ul><li>myMethod(jack); </li></ul><ul><li>myMethod(dog); </li></ul><ul><li>myMethod(mammal); </li></ul><ul><li>} </li></ul><ul><li>public static void myMethod(Mammal a) { </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  101. 101. Object parameter <ul><li>... </li></ul><ul><li>class Exercise13 { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Human jack = new Human(); </li></ul><ul><li>Dog spot = new Dog(); </li></ul><ul><li>Mammal mammal = new Mammal(); </li></ul><ul><li>// these work! You can pass every object to the method </li></ul><ul><li>myMethod(jack); </li></ul><ul><li>myMethod(dog); </li></ul><ul><li>myMethod(mammal); </li></ul><ul><li>myMethod(&quot;hello&quot;); // String </li></ul><ul><li>} </li></ul><ul><li>public static void myMethod(Object a) { </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  102. 102. Calling methods from Mammal <ul><li>class Mammal { </li></ul><ul><li>} </li></ul><ul><li>class Human extends Mammal { </li></ul><ul><li>public void bark() { System.out.println(&quot;Bark!&quot;); }; </li></ul><ul><li>} </li></ul><ul><li>class Dog extends Mammal { </li></ul><ul><li>} </li></ul><ul><li>class Exercise13 { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Human jack = new Human(); </li></ul><ul><li>Dog spot = new Dog(); </li></ul><ul><li>Mammal mammal = new Mammal(); </li></ul><ul><li>myMethod(jack); </li></ul><ul><li>myMethod(dog); </li></ul><ul><li>myMethod(mammal); </li></ul><ul><li>} </li></ul><ul><li>public static void myMethod(Mammal a) { </li></ul><ul><li>a.bark(); // Why this does not work? </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  103. 103. Solution <ul><li>class Exercise13 { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Human jack = new Human(); </li></ul><ul><li>Dog spot = new Dog(); </li></ul><ul><li>Mammal mammal = new Mammal(); </li></ul><ul><li>myMethod(jack); </li></ul><ul><li>myMethod(dog); </li></ul><ul><li>myMethod(mammal); </li></ul><ul><li>} </li></ul><ul><li>public static void myMethod(Mammal a) { </li></ul><ul><li>// Now it works </li></ul><ul><li>if(a instanceof Dog) { </li></ul><ul><li>Dog spot = (Dog) a; </li></ul><ul><li>spot.bark(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  104. 104. This works, why? <ul><li>class Mammal { </li></ul><ul><li>public void giveBirth() { System.out.println(&quot;Giving birth&quot;); }; </li></ul><ul><li>} </li></ul><ul><li>class Human extends Mammal { </li></ul><ul><li>} </li></ul><ul><li>class Dog extends Mammal { </li></ul><ul><li>} </li></ul><ul><li>class Exercise13 { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Human jack = new Human(); </li></ul><ul><li>Dog spot = new Dog(); </li></ul><ul><li>Mammal mammal = new Mammal(); </li></ul><ul><li>myMethod(jack); </li></ul><ul><li>myMethod(dog); </li></ul><ul><li>myMethod(mammal); </li></ul><ul><li>} </li></ul><ul><li>public static void myMethod(Mammal a) { </li></ul><ul><li>a.giveBirth(); // Why this works? </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  105. 105. <ul><li>class Movable { </li></ul><ul><li>public void start(); </li></ul><ul><li>public void stop(); </li></ul><ul><li>} </li></ul><ul><li>class Vehicle { </li></ul><ul><li>} </li></ul><ul><li>class Car extends Vehicle implements Movable { </li></ul><ul><li>public void start() { </li></ul><ul><li>// Do something </li></ul><ul><li>  } </li></ul><ul><li>public void stop() { </li></ul><ul><li>// Do something </li></ul><ul><li>  } </li></ul><ul><li>} </li></ul><ul><li>class Exercise13 { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Car c = new Car(); </li></ul><ul><li>myMethod(c); </li></ul><ul><li>} </li></ul><ul><li>// You can pass every object that implements the Movable! </li></ul><ul><li>public static void myMethod( Movable a ) { </li></ul><ul><li>a.start(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>

×