1.77k likes | 1.91k Views
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.
E N D
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
String History • public interface StringHistory { • } publicvoid addElement (String element); publicint size(); public String elementAt (int index);
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++; • } • } • }
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); • } • }
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 ""; } }
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();
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
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
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];
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
James Dean Joe Doe Jane Smith index 0 indexOf (String element) indexOf(“Joe Doe”); size array 4 John Smith
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
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;
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
James Dean Joe Doe Jane Smith publicvoid clear() clear() size array 4 0 public void clear() { size = 0; } John Smith
Mary Doe Joe Doe Jane Smith addElement(“Mary Doe”) size array 0 1 John Smith
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
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
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();
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() { } }
Physical Object Implicit extension Object Animal AStringHistory StringHistory String Vehicle Mammal Car Primate Regular Accord Physical and Computer Inheritance AStringDatabase Human StringDatabase Deluxe Accord
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++; • } • } • }
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++; • } • } • }
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()
IS-A Relationships Object StringHistory AStringHistory String StringDatabase AStringDatabase implements extends
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
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.
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.
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
Type Checking Examples StringHistory stringHistory = new AStringDatabase(); StringDatabase stringDatabase = new AStringHistory();
Regular Model Requested Deluxe Model Assigned Navigation System myCar.steer(); myCar. setCity(“Raleigh”); ((ADeluxeModel) myCar). setCity(“Raleigh”); Getting an Upgrade ARegularModel myCar = new ADeluxeModel ();
Deluxe Model Requested Regular Model Assigned myCar.steer(); myCar. setCity(“Raleigh”); Getting a Downgrade ADeluxeModel myCar = new ARegularModel ();
Type Checking Examples StringHistory stringHistory = new AStringDatabase(); stringHistory.size() stringHistory .clear() ((StringDatabase) stringHistory) .clear() StringDatabase stringDatabase = new AStringHistory(); stringDatabase.clear()
Type Checking Examples Object[] objects = {“Joe Doe”, new AStringDatabase(), new AStringHistory()}; String[] strings = {“Joe Doe”, new Object()};
AStringDatabase Instance AStringHistory Instance Actual Parameters of different types IS-A & Polymorphism print (StringHistory stringHistory) { … } IS-A
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( )
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++; • } • }
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(); • }
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(); • }
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( )
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”
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); • } • }
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); • } • }
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); • } • }
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); • } • }
Type of switch expression must be ordinal type Illegal Switch • switch (input ){ • case “print”: • print(names); • case “clear”: • names.clear(); • default: • names.addElement(input); • }