Advance Inheritance and Polymorphism PDF
Advance Inheritance and Polymorphism PDF
Polymorphism
Objectives
• Create and use abstract classes
• Use static and dynamic binding
• Create arrays of subclass objects
• Use the Object class and its methods
• Operation Overloading
2
Objectives (continued)
• Use inheritance to achieve good software
design
• Create and use interfaces
3
Creating and Using
Abstract Classes
• Abstract class
– Cannot create any concrete objects
– Can inherit
– Usually has one or more empty abstract methods
– Use keyword abstract
– Provide superclass from which other objects can
inherit
– Example:
public abstract class Animal
4
Creating and Using
Abstract Classes (continued)
• Abstract method does not have:
– Body
– Curly braces
– Method statements
• To create abstract method
– Keyword abstract
– Header including method type, name, and arguments
– Include semicolon at end of declaration
public abstract void speak();
5
Creating and Using
Abstract Classes (continued)
• Subclass of abstract class
– Inherits abstract method from parent
• Must provide implementation for inherited method
• Or be abstract itself
– Code subclass method to override empty
superclass method
6
Using Dynamic Method Binding
• Every subclass object “is a” superclass
member
– Convert subclass objects to superclass objects
– Can create reference to superclass object
• Create variable name to hold memory address
• Store concrete subclass object
• Example:
Animal ref;
ref = new Cow();
7
Using Dynamic Method Binding
(continued)
• Dynamic method binding
– Also called late binding
– Application’s ability to select correct subclass
method
– Makes programs flexible
• When application executes
– Correct method attached to application based on
current one
8
Using Dynamic Method Binding
(continued)
9
Using a Superclass as
a Method Parameter Type
• Use superclass as method argument
– Pass in subclass
• Use dynamic method binding
public static void talkingAnimal
(Animal animal)
Dog dog = new Dog();
talkingAnimal(dog);
10
11
Using a Superclass as
a Method Parameter Type (continued)
12
Creating Arrays of
Subclass Objects
• Create superclass reference
– Treat subclass objects as superclass objects
• Create array of different objects
• Share same ancestry
• Creates array of three Animal references
Animal[] ref = new Animal[3];
– Reserve memory for three Animal object
references
13
Using the Object Class
and Its Methods
• Object Class
– Every Java class extension of Object class
– Defined in java.lang package
– Imported automatically
– Includes methods to use or override
14
15
Using the toString() Method
• toString() method
– Converts Object into String
– Contains information about Object
– Output
• Class name
• @ sign
• Hash code
16
Using the toString() Method
(continued)
• Write overloaded version of toString()
method
– Display some or all data field values for object
– Can be very useful in debugging a program
• Display toString() value
• Examine contents
17
Using the toString() Method
(continued)
18
Using the equals() Method
• equals() method
– Takes single argument
• Same type as invoking object
– Returns boolean value
• Indicates whether objects are equal
– Considers two objects of same class to be equal
• Only if they have same hash code
19
Using the equals() Method
(continued)
• Example of equals() method:
if(someObject.equals
(someOtherObjectOfTheSameType))
System.out.println("The objects
are equal");
• To consider objects to be equal based on
contents
– Must write own equals() method
20
Using the equals() Method
(continued)
• Object method hashCode()
– Returns integer representing hash code
– Whenever you override equals() method
• Should override hashCode() method as well
• Equal objects should have equal hash codes
21
22
Using Inheritance To Achieve
Good Software Design
• Create powerful computer programs more
easily
– If components used “as is” or slightly modified
• Make programming large systems more
manageable
23
Using Inheritance To Achieve
Good Software Design (continued)
• Advantages of extendable superclasses
– Save development time
• Much code already written
– Save testing time
• Superclass code already tested
– Programmers understand how superclass works
– Superclass maintains its integrity
• Bytecode not changed
24
Creating and Using Interfaces
• Multiple inheritance
– Inherit from more than one class
– Prohibited in Java
– Variables and methods in parent classes might
have identical names
• Creates conflict
• Which class should super refer when child class has
multiple parents?
25
Creating and Using Interfaces
(continued)
• Interface
– Alternative to multiple inheritance
– Looks like a class
• Except all methods and data items implicitly public,
abstract, and final
– Description of what class does
– Declares method headers
26
UML class and interface
27
28
Creating and Using Interfaces
(continued)
29
Creating and Using Interfaces
(continued)
30
Creating and Using Interfaces
(continued)
• Create an interface
– Example: public interface Worker
• Implement an interface
– Keyword implements
• Requires subclass to implement own version of each
method
– Use interface name in class header
• Requires class objects to include code
public class WorkingDog extends Dog
implements Worker
31
Creating and Using Interfaces
(continued)
• Abstract classes versus interfaces
– Cannot instantiate concrete objects of either
– Abstract classes
• Can contain nonabstract methods
• Provide data or methods that subclasses can inherit
• Subclasses maintain ability to override inherited
methods
• Can include methods that contain actual behavior
object performs
32
Creating and Using Interfaces
(continued)
• Abstract classes versus interfaces (continued)
– Interface
• Methods must be abstract
• Programmer knows what actions to include
• Every implementing class defines behavior that must
occur when method executes
• Class can implement behavior from more than one
parent
33
Creating Interfaces to Store Related
Constants
• Interfaces can contain data fields
– Must be public, static, and final
• Interfaces contain constants
– Provide set of data that many classes can use
without having to redeclare values
34
Creating Interfaces to Store Related
Constants (continued)
35
You Do It
• Creating an abstract class
• Extending an abstract class
• Extending an abstract class with a second
subclass
• Instantiating objects from subclasses
• Using object references
36
You Do It (continued)
• Overriding the Object class equals()
method
• Eliminating duplicate user entries
• Creating a package
37
Don’t Do It
• Don’t write a body for an abstract method
• Don’t forget to end an abstract method header
with a semicolon
• Don’t forget to override any abstract methods in
any subclasses you derive
• Don’t mistakenly overload an abstract method
instead of overriding it
• Don’t try to instantiate an abstract class object
• Don’t forget to override all the methods in an
interface that you implement
38
Summary
• Abstract class
– Class that you create only to extend from, but not to
instantiate from
– Usually contain abstract methods
• Methods with no method statements
• Can convert subclass objects to superclass objects
• Dynamic method binding
– Create a method that has one or more parameters
that might be one of several types
– Create an array of superclass object references but
store subclass instances in it
39
Summary (continued)
• Interface
– Similar to a class
– All methods implicitly public and abstract
– All of its data implicitly public, static, and
final
– Create class that uses interface
• Include keyword implements and interface name in
class header
40