Polymorphism is kind powerful concept in Object-oriented programming. The following are quick overview.
Polymorphism with Java Interface..
Sometime, we may want to use polymorphism which is not really "is-a" inheritance relationship at class/object analysis and design modelling stage. That is, there won't be able to do superclass-subclass and make use of abstract class/method generalization as mentioned above. There may be somewhat constituting same behavior in given domain context. Then, we can use Java interface to still practice the polymorphism. This is somewhat better Java programming practice.
I just try to make CampusArea class to deliberately try to differentiate from Shape "is-a" generalization with Square and Circle, i.e. CampusArea should not be(is no meaningful of) subclass of Shape. And Java interface GeometricInterface is created and whichever (concrete) classes that implement it shall do the actual getArea() method implementation, and here by using abstract keyword to make it strengthen the implementation model, i.e.. concrete class 'must' implement but you may relax this if you want in your model. This basic concept also manifested into Java AWT/Swing GUI interfaces, Java multiple interface inheritance(i.e., behavior/method level, not at property) concept and independently implementable interfaces on Java collection framework.
- simply means many forms
- object variable reference
- polymorphic value, i.e. referencing many
- underlaying concept is computer memory storage i.e. how computer program store variable and referencing or pointing at
Polymorphism with Java Abstract..
Polymorphism come after study of class generalization relationship and object-oriented concepts such as Reuse, Subtype, Substitutability, Inheritance. The following depict "is-a", inheritance relationship kind of polymorphism in Java. It uses of abstract class and abstract method at Superclass to let its subclasses to be implemented getArea() method. 01 | //Shape.java |
02 | public abstract class Shape |
03 | { |
04 | protected int posX; |
05 | protected int posY; |
06 | |
07 | public Shape (int x, int y) |
08 | { |
09 | posX = x; |
10 | posY = y; |
11 | } |
12 | |
13 | public abstract double getArea(); |
14 | } |
15 |
16 | //Square.java |
17 | public class Square extends Shape |
18 | { |
19 | private int side; |
20 | |
21 | public Square (int x, int y, int s) |
22 | { |
23 | super(x,y); |
24 | side = s; |
25 | } |
26 | |
27 | public double getArea() |
28 | { |
29 | return ( side * side ); |
30 | } |
31 | } |
32 |
33 | //Circle.java |
34 | public class Circle extends Shape |
35 | { |
36 | private int radius; |
37 | |
38 | public Circle (int x, int y, int r) |
39 | { |
40 | super(x,y); |
41 | radius = r; |
42 | } |
43 | |
44 | public double getArea() |
45 | { |
46 | return (Math.PI * radius * radius); |
47 | } |
48 | } |
49 |
50 | //DemoPolymorphism.java |
51 | public class DemoPolymorphism |
52 | { |
53 | public static void main(String[] args) |
54 | { |
55 | Shape vars; |
56 | vars = new Square(1,2,3); |
57 | System.out.println("Area of square = " +vars.getArea()); |
58 | vars = new Circle(1,2,3); |
59 | System.out.println("Area of circle = " +vars.getArea()); |
60 | } |
61 | } |
Polymorphism with Java Interface..
Sometime, we may want to use polymorphism which is not really "is-a" inheritance relationship at class/object analysis and design modelling stage. That is, there won't be able to do superclass-subclass and make use of abstract class/method generalization as mentioned above. There may be somewhat constituting same behavior in given domain context. Then, we can use Java interface to still practice the polymorphism. This is somewhat better Java programming practice.
01 | //GeometricInterface.java |
02 | public interface GeometricInterface |
03 | { |
04 | public abstract double getArea(); |
05 | } |
06 |
07 | //Shape.java |
08 | public class Shape |
09 | { |
10 | protected int posX; |
11 | protected int posY; |
12 | |
13 | public Shape (int x, int y) |
14 | { |
15 | posX = x; |
16 | posY = y; |
17 | } |
18 | } |
19 |
20 | //Square.java |
21 | public class Square extends Shape implements GeometricInterface |
22 | { |
23 | private int side; |
24 | |
25 | public Square (int x, int y, int s) |
26 | { |
27 | super(x,y); |
28 | side = s; |
29 | } |
30 | |
31 | public double getArea() |
32 | { |
33 | return ( side * side ); |
34 | } |
35 | } |
36 |
37 | //Circle.java |
38 | public class Circle extends Shape implements GeometricInterface |
39 | { |
40 | private int radius; |
41 | |
42 | public Circle (int x, int y, int r) |
43 | { |
44 | super(x,y); |
45 | radius = r; |
46 | } |
47 | |
48 | public double getArea() |
49 | { |
50 | return (Math.PI * radius * radius); |
51 | } |
52 | } |
53 |
54 | //CampusArea.java |
55 | public class CampusArea implements GeometricInterface |
56 | { |
57 | private double width; |
58 | private double length; |
59 | |
60 | public CampusArea(double width, double length) |
61 | { |
62 | this.width = width; |
63 | this.length = length; |
64 | } |
65 | |
66 | public double getArea() |
67 | { |
68 | return ( width * length ); |
69 | } |
70 | } |
71 |
72 | //DemoPolymorphism.java |
73 | public class DemoPolymorphism |
74 | { |
75 | public static void main(String[] args) |
76 | { |
77 | GeometricInterface vars; |
78 | vars = new Square(1,2,3); |
79 | System.out.println("Area of square = " +vars.getArea()); |
80 | vars = new Circle(1,2,3); |
81 | System.out.println("Area of circle = " +vars.getArea()); |
82 | vars = new CampusArea(3,3); |
83 | System.out.println("Area of campus = " +vars.getArea()); |
84 | |
85 | } |
86 | } |
I just try to make CampusArea class to deliberately try to differentiate from Shape "is-a" generalization with Square and Circle, i.e. CampusArea should not be(is no meaningful of) subclass of Shape. And Java interface GeometricInterface is created and whichever (concrete) classes that implement it shall do the actual getArea() method implementation, and here by using abstract keyword to make it strengthen the implementation model, i.e.. concrete class 'must' implement but you may relax this if you want in your model. This basic concept also manifested into Java AWT/Swing GUI interfaces, Java multiple interface inheritance(i.e., behavior/method level, not at property) concept and independently implementable interfaces on Java collection framework.


0 comments
Post a Comment