Re: Question about Abstract classes and Interfaces
- From: "Steve W. Jackson" <stevewjackson@xxxxxxxxxxx>
- Date: Tue, 11 Dec 2007 11:31:02 -0600
In article
<d9640e0d-dbb8-41b4-8a5b-d5ee453b14d0@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
imenalo@xxxxxxxxx wrote:
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.
I think of an interface as a way to make any object fit the "is a"
definition. Since Circle implements HasArea, then Circle "is a" HasArea
and can be passed as a parameter anywhere a HasArea is accepted. More
generally, in this case, anything representing a geometric shape that
has an area should implement it so that you can ask for the area.
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
You can not directly instantiate the abstract class. But as a defined
class, you can use its name to refer to any instance of a subclass.
That's similar to the way that an instance of any class could be
referred to as Object, since java.lang.Object is the base class of the
entire class hierarchy in the language. When reading the Javadocs,
you'll note that the inheritance tree of the class being viewed always
goes back to Object.
The two examples above demonstrate different things. GregorianCalendar
is a concrete subclass of Calendar and has a constructor you can use to
create one. As a subclass of Calendar, you *can* refer to it as such.
The second, however, shows that the Calendar class, while abstract,
contains a static "factory method" which will create an instance some
concrete subclass and return it to you. If you wanted, you could take
that "cal" variable and call "cal.getClass().getName()" to find out the
package-qualified name of the concrete subclass, which may even be in
some package you can't access yourself.
Aside from that, however, the most common use of abstract classes is to
form an outline so that developers using some API you provide would be
required, if they want to use instances of your class, to create
subclasses containing implementation of methods you specify. I'm
allowed to implement a Calendar subclass if I like, and I *must*
implement the abstract methods or other members or I can't use it like
any other Calendar...but in this particular case, it's not done often
since the most commonly used Calendar types are available on a
per-locale basis via static factory methods.
= Steve =
--
Steve W. Jackson
Montgomery, Alabama
.
- References:
- Question about Abstract classes and Interfaces
- From: imenalo
- Question about Abstract classes and Interfaces
- Prev by Date: File creation in the web applications' root
- Next by Date: Re: Question about Abstract classes and Interfaces
- Previous by thread: Question about Abstract classes and Interfaces
- Next by thread: Re: Question about Abstract classes and Interfaces
- Index(es):
Relevant Pages
|