Inheritance and IS-A - PowerPoint PPT Presentation

inheritance and is a n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Inheritance and IS-A PowerPoint Presentation
Download Presentation
Inheritance and IS-A

play fullscreen
1 / 177
Inheritance and IS-A
113 Views
Download Presentation
oona
Download Presentation

Inheritance and IS-A

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Inheritance and IS-A • Inheritance • inheriting ancestor’s traits • inheriting benefactor’s assets • inheriting instance members( methods/variables) • IS-A Relationship • human IS-A Mammal • salmon IS-A Fish • “Joe Doe” IS-A Student • A car made by this factory is an Acura. • ABMISpreadsheet IS-A BMISpreadsheet • IS-A vs. Inheritance • Human inherits Mammal traits, therefore Human IS-A Mammal • Inheritance => IS-A but not vice versa

  2. Example

  3. String History • public interface StringHistory { • } publicvoid addElement (String element); publicint size(); public String elementAt (int index);

  4. Implementing the History • public class AStringHistory implements StringHistory { • publicfinalint MAX_SIZE = 50; • String[] contents = new String[MAX_SIZE]; • int size = 0; • publicint size() { return size;} • public String elementAt (int index) { return contents[index]; } • boolean isFull() { return size == MAX_SIZE; } • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • }

  5. Using the History • publicstaticvoid main(String[] args) { • StringHistory names = new AStringHistory(); • while (true) { • String input = readLine(); • if (input.length > 0) • if (input.charAt(0) == 'q') break; • elseif (input.charAt(0) == 'p' ) • print(names); • else • names.addElement(input); • } • }

  6. Printing a History staticvoid print(StringHistory strings) { System.out.println("******************"); for ( int elementNum = 0; elementNum < strings.size(); elementNum++) System.out.println(strings.elementAt(elementNum)); } static String readLine() { try { return inputStream.readLine(); } catch (Exception e) { System.out.println(e); return ""; } }

  7. Database

  8. Database • public interface StringDatabase { • //from history • publicint size(); • publicvoid addElement (String element); • public String elementAt (int index); • //additional methods • } publicvoid deleteElement(String element); publicboolean member (String element); publicvoid clear();

  9. James Dean Joe Doe Jane Smith Elements out of order! deleteElement (String element) deleteElement(“Joe Doe”); size array publicvoid deleteElement (String element) { contents[indexOf(element)] = contents[size - 1]; size--; } 4 3 John Smith John Smith

  10. James Dean Joe Doe Jane Smith index 1 deleteElement (String element) deleteElement(“Joe Doe”); size array publicvoid deleteElement (String element) { shiftUp(indexOf(element)); } 4 John Smith

  11. James Dean Joe Doe Jane Smith index 1 Multi-element window deleteElement(“Joe Doe”); size array publicvoid deleteElement (String element) { shiftUp(indexOf(element)); } 4 Jane Smith John Smith contents[index] = contents[index + 1];

  12. James Dean Joe Doe Jane Smith index 2 deleteElement (String element) deleteElement(“Joe Doe”); size array publicvoid deleteElement (String element) { shiftUp(indexOf(element)); } 4 3 Jane Smith void shiftUp (int startIndex) { for (int index = startIndex ; index + 1 < size; index++) contents[index] = contents[index + 1]; size--; } John Smith John Smith

  13. James Dean Joe Doe Jane Smith index 0 indexOf (String element) indexOf(“Joe Doe”); size array 4 John Smith

  14. James Dean Joe Doe Jane Smith index 1 indexOf (String element) indexOf(“Joe Doe”); size array 4 • publicint indexOf (String element) { • for ( index = 0; index < size && • !element.equals(contents[index]; • index++) • ; • return index; • } John Smith

  15. James Dean Joe Doe Jane Smith publicboolean member(String element) member(“Joe Doe”); size array • publicint indexOf (String element) { • for ( index = 0; index < size && • !element.equals(contents[index]; • index++) • ; • return index; • } 4 John Smith publicboolean member (String element) { } return indexOf (element) < size;

  16. James Dean Joe Doe Jane Smith publicvoid clear() clear() size array 4 3 2 1 0 • public void clear() { • while ( size > 0) • deleteElement(size -1); • } John Smith

  17. James Dean Joe Doe Jane Smith publicvoid clear() clear() size array 4 0 public void clear() { size = 0; } John Smith

  18. Mary Doe Joe Doe Jane Smith addElement(“Mary Doe”) size array 0 1 John Smith

  19. size size( ) size( ) String History AString History IMPLEMENTS contents elementAt( ) elementAt( ) addElement( ) MAX_SIZE addElement( ) isFull( ) size( ) size size( ) AString Database IMPLEMENTS String Database elementAt( ) contents elementAt( ) addElement( ) MAX_SIZE addElement( ) member( ) member( ) shiftUp( ) deleteElement( ) deleteElement( ) clear( ) clear( ) indexOf( ) Logical but not Physical Extensions

  20. size size( ) size( ) String History IMPLEMENTS AString History contents elementAt( ) elementAt( ) addElement( ) MAX_SIZE addElement( ) isFull( ) Supertype Superclass EXTENDS EXTENDS Subclass member( ) member( ) AString Database IMPLEMENTS String Database deleteElement( ) deleteElement( ) Subtype clear( ) indexOf( ) clear( ) shiftUp( ) Physical and Logical Extensions

  21. public interface StringHistory { • } publicvoid addElement (String element); publicint size(); public String elementAt (int index); extends/ inherits from Inherited members Extending an Interface public interface StringDatabase extends StringHistory { } publicvoid deleteElement(String element); publicvoid member (String element); publicvoid clear();

  22. Extending a Class public class AStringDatabase extends AStringHistory implements StringDatabase { publicvoid deleteElement (String element) { … } int indexOf (String element) { … } void shiftUp (int startIndex) { … } publicboolean member(String element) { } publicvoid clear() { } }

  23. Physical Object Implicit extension Object Animal AStringHistory StringHistory String Vehicle Mammal Car Primate Regular Accord Physical and Computer Inheritance AStringDatabase Human StringDatabase Deluxe Accord

  24. No Explicit Extension • public class AStringHistory implements StringHistory { • publicfinalint MAX_SIZE = 50; • String[] contents = new String[MAX_SIZE]; • int size = 0; • publicint size() { return size;} • public String elementAt (int index) { return contents[index]; } • boolean isFull() { return size == MAX_SIZE; } • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • }

  25. Equivalent Class Definition • public class AStringHistory extends Object implements StringHistory { • publicfinal int MAX_SIZE = 50; • String[] contents = new String[MAX_SIZE]; • int size = 0; • publicint size() { return size;} • public String elementAt (int index) { return contents[index]; } • boolean isFull() { return size == MAX_SIZE; } • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • }

  26. Used by println() Some Methods of Class Object toString( ) size size( ) EXTENDS AString History Object contents elementAt( ) equals( ) MAX_SIZE addElement( ) clone( ) isFull( ) (new AStringHistory()).toString() EXTENDS (new AStringDatabase()).toString() member( ) AString Database (new ACartesianPoint()).toString() deleteElement( ) “hello”.toString() indexOf( ) ‘h’.toString() clear( ) shiftUp( ) 5.toString()

  27. IS-A Relationships Object StringHistory AStringHistory String StringDatabase AStringDatabase implements extends

  28. AStringHistory Instance AStringDatabase Instance Printing a History staticvoid print(StringHistory strings) { System.out.println("******************"); for ( int elementNum = 0; elementNum < strings.size(); elementNum++) System.out.println(strings.elementAt(elementNum)); } AStringHistory IS-A StringHistory AStringDatabase IS-A AStringHistory AStringDatabase IS-A StringHistory

  29. Assignment Rules for Primitive Types • If T1 narrower than T2 (Set of instances of T1  Set of instances of T2) • Expression of type T1 can be assigned to Variable of type T2 • Expression of type T2 can be assigned to Variable of type T1 with cast.

  30. Assignment Rules for Object Types • If T1 IS-A T2 • Expression of type T1 can be assigned to Variable of type T2 • Expression of type T2 can be assigned to Variable of type T1 with cast.

  31. IS-A Definition • Implements: T1 implements T2 => T1 IS-A T2 • Extends: T1 extends T2 => T1 IS-A T2 • Transitive: • T1 IS-A T2 • T2 IS-A T3 • => T1 IS-A T3 • Reflexive: • T1 == T2 => T1 IS-A T2

  32. Type Checking Examples StringHistory stringHistory = new AStringDatabase(); StringDatabase stringDatabase = new AStringHistory();

  33. Regular Model Requested Deluxe Model Assigned Navigation System myCar.steer(); myCar. setCity(“Raleigh”); ((ADeluxeModel) myCar). setCity(“Raleigh”); Getting an Upgrade ARegularModel myCar = new ADeluxeModel ();

  34. Deluxe Model Requested Regular Model Assigned myCar.steer(); myCar. setCity(“Raleigh”); Getting a Downgrade ADeluxeModel myCar = new ARegularModel ();

  35. Type Checking Examples StringHistory stringHistory = new AStringDatabase(); stringHistory.size() stringHistory .clear() ((StringDatabase) stringHistory) .clear() StringDatabase stringDatabase = new AStringHistory(); stringDatabase.clear()

  36. Type Checking Examples Object[] objects = {“Joe Doe”, new AStringDatabase(), new AStringHistory()}; String[] strings = {“Joe Doe”, new Object()};

  37. AStringDatabase Instance AStringHistory Instance Actual Parameters of different types IS-A & Polymorphism print (StringHistory stringHistory) { … } IS-A

  38. Database

  39. Set

  40. Overriden Method AString Set Overriding Method Overriding Inherited Methods toString( ) size size( ) EXTENDS AString History Object contents elementAt( ) equals( ) MAX_SIZE addElement( ) clone( ) isFull( ) EXTENDS member( ) AString Database addElement( ) deleteElement( ) indexOf( ) clear( ) shiftUp( )

  41. Overriding addElement() • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • publicvoid addElement(String element) { • if (member(element)) return; • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • }

  42. inherited addElement() super • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • publicvoid addElement(String element) { • if (member(element)) return; • super.addElement(); • }

  43. Recursive call Omitting Super • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • publicvoid addElement(String element) { • if (member(element)) return; • addElement(); • }

  44. Overriden Method AString Set Overriding Method More Overriding toString( ) size size( ) EXTENDS AString History Object contents elementAt( ) equals( ) MAX_SIZE addElement( ) clone( ) isFull( ) EXTENDS member( ) AString Database addElement( ) deleteElement( ) indexOf( ) toString( ) clear( ) shiftUp( )

  45. More Overriding stringSet.toString()  “AStringSet@1eed58” public String toString() { String retVal = “”; for (int i = 0; i < size; i++) retVal += “:” + contents[i]; return retVal; } stringSet.toString()  “:James Dean:John Smith”

  46. Main class • public static void main(String args[]) { • StringDatabase names = new AStringDatabase(); • while (true) { • String input = Keyboard.readLine(); • if (!(input.length() == 0)) • if (input.charAt(0) == 'q') • break; • elseif (input.charAt(0) == 'p') • print(names); • elseif (input.charAt(0) == 'd') • names.deleteElement(input.substring(2, input.length())); • elseif (input.charAt(0) == 'm') • System.out.println(names.member(input.substring(2, input.length()))); • elseif (input.charAt(0) == 'c') • names.clear(); • else • names.addElement(input); • } • }

  47. Breaks out of the loop Switch expression Breaks out of the switch Switch arm Switch case (value of switch expressiuon) Main with Switch • public static void main(String args[]) { • StringDatabase names = new AStringDatabase(); • while (true) { • String input = Keyboard.readLine(); • if (!(input.length() == 0)) • if (input.charAt(0) == 'q') • break; • else switch (input.charAt(0)) { • case 'p': • print(names); • break; • case 'd': • names.deleteElement(input.substring(2, input.length())); • break; • case 'm': • System.out.println(names.member(input.substring(2, input.length()))); • break; • case 'c': • names.clear(); • break; • default: • names.addElement(input); • } • }

  48. Multi-case arms • public static void main(String args[]) { • StringDatabase names = new AStringDatabase(); • while (true) { • String input = Keyboard.readLine(); • if (!(input.length() == 0)) • if (input.charAt(0) == 'q') • break; • else switch (input.charAt(0)) { • case 'p’, ‘P’: • print(names); • break; • case 'd’, ‘D’: • names.deleteElement(input.substring(2, input.length())); • break; • case 'm’, ‘M’: • System.out.println(names.member(input.substring(2, input.length()))); • break; • case 'c’, ‘C’: • names.clear(); • break; • default: • names.addElement(input); • } • }

  49. Omitting break • public static void main(String args[]) { • StringDatabase names = new AStringDatabase(); • while (true) { • String input = Keyboard.readLine(); • if (!(input.length() == 0)) • if (input.charAt(0) == 'q') • break; • else switch (input.charAt(0)) { • case 'p’, ‘P’: • print(names); • case 'd’, ‘D’: • names.deleteElement(input.substring(2, input.length())); • case 'm’, ‘M’: • System.out.println(names.member(input.substring(2, input.length()))); • case 'c’, ‘C’: • names.clear(); • default: • names.addElement(input); • } • }

  50. Type of switch expression must be ordinal type Illegal Switch • switch (input ){ • case “print”: • print(names); • case “clear”: • names.clear(); • default: • names.addElement(input); • }