2. A quick review in inheritance and polymorphism
1. We have avoided code duplication
2. We have overridden some methods that need
specific implementation by the subclasses
3. We created a polymorphic design, so that we
can use Animal references as arguments for any
subtype class even those that haven’t been
created yet.
4. By establishing a common protocol in the
inheritance tree we buy more extendibility for
our code-design.
3. Abstract and concrete classes
• It doesn’t make sense for some
superclasses to be instantiated.
• We still need superclasses for the
purpose of inheritance and
polymorphism but it has no meaning a
generic use of them.
• There is a simple way to stop a class
from being instantiated and that is the
declaration abstract
• Abstract classes can still be used as
references.
• Only the less abstract subclasses -the
concrete classes -should be
Instantiated.
Syntax
abstract class Canine extends Animal {
public void Roam() { }
}
4. Abstract methods
• You might decide that some behaviors of an abstract class don’t make sense unless
they’re implemented by subclasses.
• An abstract class means the class must be extended.
• An abstract method means the method must be overridden.
• An abstract method has no body .
• If you declare an abstract method, you MUST mark the class abstract as well. You
can’t have an abstract method in a non-abstract class.
• An abstract class can still have non-abstract methods.
SYNTAX
public abstract void eat();
5. You must implement all abstract methods
Implementing an abstract method is like overriding a method
So you have to create a non-abstract method with the same signature and a return
type that is compatible to the return type of the abstract method.
The first concrete class in the inheritance tree must provide a body to an abstract
class
However it can avoid this “responsibility” by being abstract itself.
6. Polymorphism in action
Lets say we want to build our
own ArrayList that holds Dog
objects
We’ll use an array of dogs
Implement an add method
7. What about if we want to keep Cats too
1. Make a separate class , MyCatList to hold
Cat objects
2. Make a single class DogAndCatList that
keeps two different arrays as instance
variables and has two add methods
3. Make an Animal List class, that takes any
Animal subtype
8. Can we make a class generic enough to
take anything?
Every class in Java extends class
Object the superclass of everything
We were writing subclasses of class
Object from the beginning of time.
Without a common superclass we
wouldn't be able to create our
custom types of classes
9. Using polymorphic references of type Object has a price
Everything comes out of an ArrayList<Object> as a reference of type Object regardless of
what the actual object is , or what the reference type was when you added the object to the
list
10. Some further implications
Object o = al.get(index);
int i = o.hashCode();
o.bark(); // it won’t compile
Is it a problem to have to use an Object
reference variable to refer to a subclass object?
The compiler decides whether you
can call a method based on the
reference type, not the actual
object type.
11. A first glance at the object form in the Heap
• An object contains everything it inherits
from each of its superclasses .That means
that every object is also an instance of
class Object so it can be treated that way.
• If a reference is like a remote control, the
remote control takes on more buttons as
you move down the inheritance tree. So
an Object reference has fewer buttons
than the subtype reference “remote
control”
12. Casting an object reference back to its real type.
If you are not sure of the class subtype you can
use the instanceof operator to check.
if (o instanceof Dog) {
Dog d = (Dog) o;
}
• If you need to treat the object as its subtype
you need a reference (remote control) to
access all its specific methods.
• You can make a new subtype reference to
the object by copying the Object reference
and forcing that copy to go into the subtype
reference variable
• The above procedure is called cast.
13. Specifications changed. Is our design reusable?
• Imagine that you have to use your previous design for a PetShop program.
• You have to implement Pet behaviors like beFriendly() and play().
• You could simply add some methods more methods without breaking any
existing method implementation.
• But where would you implement the new methods and why? What are the
possible problems that can occur?
Let’s see look closer to some scenarios
17. What the ideal
approach would be?
A way to have pet behavior in just the
pet classes
A way to guarantee that all pet classes
have all of the same methods defined
(same name, same arguments, same
return types, no missing methods,
etc.), without having to cross your
fingers and hope all the programmers
get it right.
A way to take advantage of
polymorphism so that all pets can
have their pet methods called, without
having to use arguments, return types,
and arrays for each and every pet
class.
18. The previous class
diagram is not permitted
• Multiple Inheritance is not
supported in Java since it can
lead to severe problems
• The most serious is the so
called Deadly Diamond of
Death.
So how do we handle the
whole thing with Pet/Animal
class?
19. Interface gives us the solution
• A Java interface solves multiple
inheritance problem by giving the
polymorphic benefits without the
DDD.
• The way that interfaces elect to
side-step DDD is by keeping all
the methods abstract.
• That way the subclass that has to
play that role must implement all
the methods (provide body)
SYNTAX
To DEFINE an interface:
public interface Pet {...}
To IMPLEMENT an interface:
public class Dog extends Canine implements Pet {...}
20. Advantages and characteristics of an Interface
• Interfaces buy us polymorphism and flexibility.
• You can pass anything that implements an interface as an argument or a return type.
• Even from a completely different inheritance tree
• You treat an object by the role it plays and not the class that it was instantiated.
• A class can implement multiple interfaces
21. Things to think over when you design your class diagram
How do you know whether to make a class, a
subclass, an abstract class, or an interface?
1. Make a class that doesn’t extend
anything(other than Object) when your
new class doesn’t pass the IS-A test for
any other type. Make a subclass (in other
words, extend a class)only when you
need to make a more specific version of
a class and need to override or add new
behaviors.
2. Use an abstract class when you want to
define a template for a group of
subclasses, and you have at least some
implementation code that all subclasses
could use. Make the class abstract when
you want to guarantee that nobody can
make objects of that type.
3. Use an interface when you want to
define a role that other classes can play,
regardless of where those classes are in
the inheritance tree.