Abstract Classes and Interfaces Chapter 10 CPSC 150
Abstract Classes and Interfaces Chapter 10 CPSC 150
Directory Example (note: your assignment does not have a print method) Directory. Entry name phone public void print( ) { } Student Home Address School Address public void print ( ) { } Sample Entry: subclass Student superclass Susie Smith 123 Main Street subclass 23 York River East (757) 234 -6345 subclass superclass CPSC 150
Directory. Entry print (note: your assignment does not have a print method) public class Directory. Entry { private String name; private String phone; public void print( ) { System. out. println( “Name: “ + name + “n. Phone: “ + phone); } } CPSC 150
Student print (note: your assignment does not have a print method) public class Student extends Directory. Entry { private String home. Address; private String school. Address; public void print( ) { System. out. println(“Student: “ + “n. Home: “ + home. Address + “n. School: “ + school. Address); } } CPSC 150
Directory no name! public class Directory { Array. List<Directory. Entry> dir; // methods to create and fill dir Student public void print( ) Home: 123 Main Street { School: 23 York River for (int i=0; i<dir. size(); i++) { East Directory. Entry de = dir. get(i); de. print( ); // will not work for assignment // will call subclass print only } } } CPSC 150
Moral • Java is polymorphic • For syntax check, use parent class • For runtime, child class will be called • can’t get to parent class in client during runtime CPSC 150
Solution: fix Student private String home. Address; private String school. Address; private access in parent; can’t use public void print( ) { System. out. println( “Student: “ + name + “n. Phone: “ + phone + “n. Home: “ + home. Address + “n. School: “ + school. Address); } CPSC 150
Solution Try #1 to Problem #2 • call parent then child public void print( ) { super. print( ); System. out. println(“Student: “ + “n. Home: “ + home. Address + “n. School: “ + school. Address); } Susie Smith – Problem now: print doesn’t look good Phone: (757) 234 -6345 Student: Home: 123 Main Street School: 23 York River East CPSC 150
Solution #2 • use protected String name; protected String phone – allows child access, but not others – solves the problem, but frowned on • use accessor methods – works, but allows access that may not be desired • use protected accessor methods: CPSC 150
Solution: Protected access methods! public class Directory. Entry { private String name; private String phone; protected String get. Name( ) { return name; } protected String get. Phone( ) {return phone; } public class Student extends Directory. Entry { //private instance fields public void print( ) { System. out. println( “Student: “ + super. get. Name( ) + “n. Phone: “ + super. get. Phone( ) + “n. Home: “ + home. Address + “n. School: “ + school. Address); } CPSC 150
Now, Directory. Entry print isn’t needed • remove it (code not shown), but now: public class Directory { Array. List<Directory. Entry> dir; // methods to create and fill dir public void print( ) { for (int i=0; i<dir. size(); i++) { Directory. Entry de = dir. get(i); de. print( ); } // will cause syntax error if // Directory. Entry has no print method } } CPSC 150
Yet another problem • Need print method for syntax checker • Don’t need print method for runtime • Solution: Leave it in there. • Problem: Bad design CPSC 150
Final Solution: Abstract Methods • Make print method abstract – means it cannot be called/used • Leaves it there for compiler • Shows that it will not be used for design CPSC 150
Abstract Classes • If a method is abstract, it cannot be called • So, an object of that class can’t exist • So any class that has an abstract method MUST be abstract • Abstract classes can have any mix of concrete and abstract methods • Concrete classes can be called by children objects of the class CPSC 150
Syntax of abstract methods abstract public class My. Class { // regular constructors and methods that are called in a regular way abstract method signature ; //no body. NO {}s } CPSC 150
Why Abstract Methods • Abstract Methods require any children to implement that method • Compile time error if abstract method not in subclass • Allow clients that use the code to compile with the guarantee that method will be implemented CPSC 150
Your turn • Write the abstract class Directory. Entry. Make the print method abstract Note: Directory. Entry will NOT be abstract for your assignment CPSC 150
Inheritance Review private in parent is not accessible to children; protected is class Sub. Class 1 extends Super. Class can use methods from Sub. Class 1 or Super. Class Subclasses can have only one parent No different than any other class. Has no access to or information about subclasses first line of constructor is super( ); CPSC 150
Inheritance Review Two reasons for inheritance 1. To use the methods of the parent 2. To use polymorphism (e. g. add Directory. Entry to a directory, but each entry is a student, faculty or staff; directory doesn’t have to know which) CPSC 150
Abstract Classes Review • Abstract methods enable polymorphism, but have no body • Classes with abstract methods must be abstract • Abstract classes can not have objects created from them • Abstract classes can have useful concrete methods (e. g. , get. Name) and fields (e. g. , name, phone) CPSC 150
Abstract Class Review Two reasons for Abstract Classes 1. Enables polymorphism when methods are not appropriate for superclass (e. g. , draw in Shapes or print in Directory. Entry) 2. Enforces a specification (in order to be a Directory. Entry, you must have a print method) CPSC 150
Interfaces CPSC 150
One more detail: Interfaces • Allow multiple inheritance – Be an animal AND black (other things are animals and other things are black) • Can specify exactly what is needed for a concrete class – action. Performed – Comparable CPSC 150
Interfaces To implement • in interface (parent), put: – public interface My. Interface instead of public class My. Interface • in class using interface (child), put: – public class Sub. Class 1 extends Super. Class implements My. Interface CPSC 150
Two arrows CPSC 150
Interfaces • Just like a class, – but no variables (can have static final public fields) – no bodies for ANY method • Like abstract class on speed • Purpose? – polymorphism – specification CPSC 150
Interface Example • Comparable CPSC 150
Abstract Classes vs Interfaces • Use interfaces when possible because there can lots of classes following "implements" but only one "extends" • Use interfaces if you do not want any fields or methods in your parent class • Use abstract classes if you want subclasses to use common defined methods or fields CPSC 150
• Look at the code below. You have five types (classes or interfaces) U, G, B, Z, and X, and a variable of each of these types. What can you say about the relationships of the types? U u; G g; B b; Z z; X x; The following assignments are all legal: u = z; x = b; g = u; x = u; The following assignments are all illegal (they cause compiler errors): u = b; x = g; b = u; z = u; g = x; Write the class/interface headers for U, G, Z and X CPSC 150
- Slides: 29