Friday, January 25, 2013

Looking closely into MultiFunction



All Java objects will have at least one type, but usually it has more than one. The MultiFunction object we have created actually has five. It is a Copier, Printer, Phone, MultiFunction and ajava.lang.Object. The Copier, Printer and Phone types are easy to see, they were explicitly implemented by MultiFunction, so that's three. The fourth one (MultiFunction) is not very obvious, but whenever you create a new class, you are creating a new Type---hence, whatever object you will create out of that class will also bear its type. The fifth one (java.lang.Object) is because no matter what you do, at some point, your class will extend the Object class, either directly or indirectly.

Polymorphism

You will notice that the MultiFunction object needed to re-define all the methods it has inherited from the three interfaces. We had to re-define print(), copy, answerCall() and dialNumber(). Re-defining a method in a subtype is called method overriding.
Overriding a method is done in order for the inheriting class to have a chance to define a unique behavior for the method. For example, the MultiFunction object inherited the copy() from theCopier interface. What it effectively means is that MultiFunction is making a promise that because it is now a Copier object, it will respond to the copy() message if invoked. The Copier interface did not specify any behavior for the method, it is up to the implementing class (MultiFunction) to provide the behavior. The provision of behavior for an inherited method can be done using method overriding.

//Shape.java
interface Shape {
    void draw();
}

class Square implements Shape {
    public void draw() {
        System.out.println("Drawing a square");
    }
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

class TestShape {

    public static void main(String[] args) {
        Shape square = new Square();
        Shape circle = new Circle();
        square.draw();
        circle.draw();
    }
}
The Shape program is a very trivial and non-realistic example, but the simplicity should aid our understanding of why we use overriding.
The Shape type could have been written as a class. The Square and Circle classes could haveextended a Shape class instead of implemented a Shape interface---why didn't we do that? Because:
  1. Shape is not a real object (even in the real world). Shape is a name we give to describe the contour of something, the state of something---it is not something. Just like Animal is the classification we give to cats and dogs. A cat is a real thing, a dog is a real thing, an animal is not---Animal is a Type
  2. If you think about it hard enough, can Shape really draw something, will it make sense?
  3. It makes sense that Circle and Square descend from a common type, after all Circle and Square are---Shapes
The draw() method are both available in Circle and Square, but they have different implementations or behavior (method body). This was possible because we have overriden the draw() method where it makes sense. It makes the method polymorphic, it changes its behavior depending on which object you are calling it against.


Call me and you will get the knowledge and skills you need instantly.
For inquiries contact me on my mobile number or email me at inquiry@eglobiotraining.com. View my multiply site http://erwinglobio.multiply.com
Prof. Erwin M. Globio, MSIT
Senior IT Lecturer
Far Eastern University
Email Address: inquiry@eglobiotraining.com
Skype: erwinglobio
Call Now:
SMART: 09393741359
SUN: 09323956678





No comments:

Post a Comment