Abstract Classes and Interfaces - PowerPoint PPT Presentation

Loading...

PPT – Abstract Classes and Interfaces PowerPoint presentation | free to download - id: 1e6ff2-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Abstract Classes and Interfaces

Description:

Susie Smith. 123 Main Street. 23 York River East (757) 234-6345. Student. Home Address ... (note: your assignment does not have a print method) public class ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 30
Provided by: K2127
Learn more at: http://www.pcs.cnu.edu
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Abstract Classes and Interfaces


1
Abstract Classes and Interfaces
  • Chapter 10

2
Directory Example(note your assignment does not
have a print method)
DirectoryEntry name phone public void print( )

Sample Entry Student Susie Smith 123 Main
Street 23 York River East (757) 234-6345
subclass
superclass
subclass
subclass
Student Home Address School Address public void
print ( )
superclass
3
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)

4
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)

5
Directory
  • public class Directory
  • ArrayListltDirectoryEntrygt dir
  • // methods to create and fill dir
  • public void print( )
  • for (int i0iltdir.size()i)
  • DirectoryEntry de dir.get(i)
  • de.print( ) // will not work for
    assignment
  • // will call subclass print only

6
Moral
  • Java is polymorphic
  • For syntax check, use parent class
  • For runtime, child class will be called
  • cant get to parent class in client during runtime

7
Solution fix Student print
  • private String homeAddress
  • private String schoolAddress
  • public void print( )
  • System.out.println( Student
  • name \nPhone phone
  • \nHome homeAddress \nSchool
  • schoolAddress)

8
Solution Try 1 to Problem 2
  • call parent then child
  • public void print( )
  • super.print( )
  • System.out.println(Student
  • \nHome homeAddress \nSchool
  • schoolAddress)
  • Problem now
  • print doesnt look good

Susie Smith Phone (757) 234-6345 Student Home
123 Main Street School 23 York River East
9
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

10
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)

11
Now, DirectoryEntry print isnt needed
  • remove it (code not shown), but now
  • public class Directory
  • ArrayListltDirectoryEntrygt dir
  • // methods to create and fill dir
  • public void print( )
  • for (int i0iltdir.size()i)
  • DirectoryEntry de dir.get(i)
  • de.print( )
  • // will cause syntax error if
  • // DirectoryEntry has no print method

12
Yet another problem
  • Need print method for syntax checker
  • Dont need print method for runtime
  • Solution Leave it in there.
  • Problem Bad design

13
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

14
Abstract Classes
  • If a method is abstract, it cannot be called
  • So, an object of that class cant 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

15
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

16
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

17
Your turn
  • Write the abstract class DirectoryEntry. Make
    the print method abstract
  • Note DirectoryEntry will NOT be abstract for
    your assignment

18
Inheritance Review
No different than any other class. Has no access
to or information about subclasses
private in parent is not accessible to children
protected is
class SubClass1 extends SuperClass
first line of constructor is super( )
Subclasses can have only one parent
can use methods from SubClass1 or SuperClass
19
Inheritance Review
  • Two reasons for inheritance
  • To use the methods of the parent
  • To use polymorphism (e.g. add DirectoryEntry to a
    directory, but each entry is a student, faculty
    or staff directory doesnt have to know which)

20
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)

21
Abstract Class Review
  • Two reasons for Abstract Classes
  • Enables polymorphism when methods are not
    appropriate for superclass (e.g., draw in Shapes
    or print in DirectoryEntry)
  • Enforces a specification (in order to be a
    DirectoryEntry, you must have a print method)

22
Interfaces
23
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
  • actionPerformed
  • Comparable

24
Interfaces
  • To implement
  • in interface (parent), put
  • public interface MyInterface
  • instead of
  • public class MyInterface
  • in class using interface (child), put
  • public class SubClass1 extends SuperClass
    implements MyInterface

25
Two arrows
Two arrows
26
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

27
Interface Example
  • Comparable

28
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

29
  • 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
  • Write the class/interface headers for U, G, Z and
    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
About PowerShow.com