I
imenalo
Hello,
This is my first post and I'll try to make it as clear as possible.
I'm relatively new to Java, having a wonderful time learning the
language. One thing I do have trouble seeing in my mind is the use of
Interfaces and abstract classes.
The question I have regarding interfaces is, what exactly is the main
benefit to them. All the books or pages I read about the topic don't
explain it to the point where I fully understand the benefit. The
examples I see most are:
<<interface>>
HasArea
-------------------
+ getArea() double
Rectangle, Cylinder and Circle classes all implement the interface.
Therefore, they have to provide a method body for getArea(). I guess
my question remains, what use is the interface (other then applying a
rule like the one above), if all the clients (users of that interface)
need to apply their own implementation? I mean the only other benefit
I see is the ability to do the following:
HasArea shape = new Rectangle(3, 4); // or some such, makes it
convenient to track all objects that implement the interface.
Secondly, about Abstract classes. The one question I have about them
is, we can't instantiate an abstract class, but we can get an instance
to it. Using an example, we can either:
Calendar cal = new GregorianCalendar(); // use the subclass
or Calendar cal = Calendar.getInstance(); // use the factory method
The question I have, if we can't instantiate the class, what exactly
is the second implementation returning? It can't be a calendar object
can it? I mean the first one returns the reference to a
GregorianCalendar object. I just have no clue what the second one
will point to exactly.
Anyways, that's about it. Thanks in advance and have a great day.
Momar
This is my first post and I'll try to make it as clear as possible.
I'm relatively new to Java, having a wonderful time learning the
language. One thing I do have trouble seeing in my mind is the use of
Interfaces and abstract classes.
The question I have regarding interfaces is, what exactly is the main
benefit to them. All the books or pages I read about the topic don't
explain it to the point where I fully understand the benefit. The
examples I see most are:
<<interface>>
HasArea
-------------------
+ getArea() double
Rectangle, Cylinder and Circle classes all implement the interface.
Therefore, they have to provide a method body for getArea(). I guess
my question remains, what use is the interface (other then applying a
rule like the one above), if all the clients (users of that interface)
need to apply their own implementation? I mean the only other benefit
I see is the ability to do the following:
HasArea shape = new Rectangle(3, 4); // or some such, makes it
convenient to track all objects that implement the interface.
Secondly, about Abstract classes. The one question I have about them
is, we can't instantiate an abstract class, but we can get an instance
to it. Using an example, we can either:
Calendar cal = new GregorianCalendar(); // use the subclass
or Calendar cal = Calendar.getInstance(); // use the factory method
The question I have, if we can't instantiate the class, what exactly
is the second implementation returning? It can't be a calendar object
can it? I mean the first one returns the reference to a
GregorianCalendar object. I just have no clue what the second one
will point to exactly.
Anyways, that's about it. Thanks in advance and have a great day.
Momar