Dynamic Binding and Casting for the Extensibility of OOP

来源:互联网 发布:淘宝子账号不能登录 编辑:程序博客网 时间:2024/06/05 14:32

We all know that among the facts that make OOP so popular is its ability of Extensibility. So i want to see how does the JAVA program realize its exitensibility and the analysis between them.


1. Casting

package test;public class Animal {public String name;Animal(String name){this.name = name;}}class Cat extends Animal{public String eyeColor;Cat(String name, String eyeColor){super(name);this.eyeColor = eyeColor;}}class Dog extends Animal{public String furColor;Dog(String _name, String _furColor){super(_name);this.furColor = _furColor;}}// there is no f() method in base calss or subclass, we write it in another calss and passing the instance as baseclass' type  //  to that class and tell which type is the instance and do some specified work inside of if segment.

package test;public class TestA {public static void main(String [] args){TestA test = new TestA();Animal a = new Animal("animal");Cat cat = new Cat("catname","blue");Dog dog = new Dog("dogname","yellow");test.f(a);test.f(cat);test.f(dog);}public void f(Animal a){System.out.println("name: "+ a.name);if(a instanceof Cat){Cat cat = (Cat)a;System.out.println("  eyeColor:"+cat.eyeColor);}else if(a instanceof Dog){Dog dog = (Dog)a;System.out.println("  furColor:"+dog.furColor);}}}

Casting can increas the extensibility at some point. If we  want to add another object, say bird. we just need to create an object Bird extends from Animal. and in the method f() in class TestA we have to decide if a is instance of Bird and then write code inside the if segment.  It can increase extensibility because we don't need to write a lot of methods f(). We can use the base argument (here is Animal) as the parameter, and we can put a lot of its subclass's instance as the parameter, then use "instanceof " to tell what's the exact type of the instance and do some specific code inside the if segment.

I'ts characteristic is we don't have methods in the class or subclass, we write the method f( Animal a ) in another class. But it's not the best way. We can write the method f() and rewrite it in all its subclass. this way we don't need to write the method f() in class TestA, we just need to all the instance's method and the system will know which method ( baseclass's method f() or subclass's method f() ) to use at the executing time. This is called "Dynamic Binding" and this characteristic make the most of program extensibility and this is the reason why OOP is so pupular.

For example:

package test;public class Animal {public String name;Animal(String name){this.name = name;}public void enjoy(){System.out.println("animal is happy");}}class Cat extends Animal{public String eyeColor;Cat(String name, String eyeColor){super(name);this.eyeColor = eyeColor;}public void enjoy(){System.out.println("cat is happy");}// rewrite the method in subclass}class Dog extends Animal{public String furColor;Dog(String _name, String _furColor){super(_name);this.furColor = _furColor;}public void enjoy(){System.out.println("dog is happy");}// rewrite the meothd in subclass}class Boy{public String name;public Animal pet;public Boy(String _name, Animal _pet){this.name = _name;this.pet = _pet;}public void petEnjoy(){pet.enjoy();}}

package test;public class TestA {public static void main(String [] args){TestA test = new TestA();Animal a = new Animal("animal");Cat cat = new Cat("catname","blue");Dog dog = new Dog("dogname","yellow");Boy boy = new Boy("nick",cat);boy.petEnjoy();}// we don't need to write method in this class anymore!!!/**public void f(Animal a){System.out.println("name: "+ a.name);if(a instanceof Cat){Cat cat = (Cat)a;System.out.println("  eyeColor:"+cat.eyeColor);}else if(a instanceof Dog){Dog dog = (Dog)a;System.out.println("  furColor:"+dog.furColor);}}**/}
As shown in the program above, we define enjoy() method in every subclass and baseclass we didn't write the method in class TestA. At runtime the system will knows which enjoy methods will be used. Since in the constructor method of Boy, we put the subclass's instance cat as the parameter of Animal, the system will call Cat class' enjoy() method.

So in the future if we want to add another animal called Bird. We don't need to change anything in TestA, We may just need to add:

Bird bird = new Bird("birdname","black");Boy boy = new Boy("nick", bird);


In this way, OOP makes the most use of Extensition.

Dynamic binding has 3 key facters:

First: we must have extention

Second: we must rewrite base class' method in the subclass

Third: giving an subclass' instance to an base class's type

原创粉丝点击