CPSC150 Abstract Classes Chapter 10
CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void print( ) { } Sample Entry: Student Susie Smith 123 Main Street York River East (757) Student Home Address School Address public void print ( ) { } subclass superclass subclass superclass
CPSC150 DirectoryEntry print (note: your assignment does not have a print method) public class DirectoryEntry { private String name; private String phone; public void print( ) { System.out.println( “Name: “ + name + “\nPhone: “ + phone); }
CPSC150 Student print (note: your assignment does not have a print method) public class Student extends DirectoryEntry { private String homeAddress; private String schoolAddress; public void print( ) { System.out.println(“Student: “ + “\nHome: “ + homeAddress + “\nSchool: “ + schoolAddress); }
CPSC150 will call subclass print only public class Directory { ArrayList dir; // methods to create and fill dir public void print( ) { for (int i=0;i<dir.size();i++) { DirectoryEntry de = dir.get(i); de.print( ); } Directory (note: your Directory is database, not printed) Student Home: 123 Main Street School: 23 York River East no name!
CPSC150 which print DirectoryEntry objects will call DirectoryEntry print Student objects will call Student print Student print overrides DirectoryEntry print both Student and DirectoryEntry types are in ArrayList which prints depends on what type the element is. determined at runtime
CPSC150 Polymorphism For DirectoryEntry, print method prints name and phone For Student, print method prints home address and school address Which is printed is determined at compile time wikipedia: "polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behaviour. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding). "objectstypes methods dynamic binding
CPSC150 Problem Call both Student AND DirectoryEntry to get print from both
CPSC150 Solution: fix Student print private String homeAddress; private String schoolAddress; public void print( ) { System.out.println( “Student: “ + name + “\nPhone: “ + phone + “\nHome: “ + homeAddress + “\nSchool: “ + schoolAddress); } private access in parent; can’t use
CPSC150 Solution Try #1 call parent then child public void print( ) { super.print( ); System.out.println(“Student: “ + “\nHome: “ + homeAddress + “\nSchool: “ + schoolAddress); } –Problem now: print doesn’t look good Susie Smith Phone: (757) Student: Home: 123 Main Street School: 23 York River East
CPSC150 Solution #2 use protected 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
CPSC150 Solution: Protected access methods ! public class DirectoryEntry { private String name; private String phone; protected String getName( ) { return name; } protected String getPhone( ) {return phone; } public class Student extends DirectoryEntry { //private instance fields public void print( ) { System.out.println( “Student: “ + super.getName( ) + “\nPhone: “ + super.getPhone( ) + “\nHome: “ + homeAddress + “\nSchool: “ + schoolAddress); }
CPSC150 Now, DirectoryEntry print isn’t needed remove it (code not shown), but now: public class Directory { ArrayList dir; // methods to create and fill dir public void print( ) { for (int i=0;i<dir.size();i++) { DirectoryEntry de = dir.get(i); de.print( ); } // will cause syntax error if // DirectoryEntry has no print method }
CPSC150 Yet another problem Need print method for syntax checker Don’t need print method for runtime Solution: Leave it in there. Problem: Bad design
CPSC150 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
CPSC150 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
CPSC150 Syntax of abstract methods abstract public class MyClass { // regular constructors and methods that are called in a regular way abstract method signature ; //no body. NO {}s }
CPSC150 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 that method will be implemented
CPSC150 Your turn Write the abstract class DirectoryEntry. Make the print method abstract Note: you will not have a print method for your assignment
CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class SubClass1 extends SuperClass first line of constructo r is super( ); can use methods from SubClass1 or SuperClass private in parent is not accessible to children; protected is Subclasses can have only one parent subclass can override methods in parent. overriden methods can be accessed by super.method( ); which method is called is determined by polymorphis m at runtime
CPSC150 Inheritance Review Two reasons for inheritance 1.To use the methods of the parent 2.To use polymorphism (e.g. add DirectoryEntry to a directory, but each entry is a student, faculty or staff; directory doesn’t have to know which)
CPSC150 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., getName) and fields (e.g.,name, phone)
CPSC150 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 DirectoryEntry) 2.Enforces a specification (in order to be a DirectoryEntry, you must have a print method)