Re: Question about Abstract classes and Interfaces



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
.



Relevant Pages

  • Re: How about "pure virtual methods"?
    ... doesn't implement all the interface. ... based on the existence of "isinstance" in Python. ... subclass of a given type. ... So I conclude that a subclass, in Python, must implement the interface ...
    (comp.lang.python)
  • Re: iPod Touch
    ... interface is very nice - not quite as fluid as one might be led to ... Calendar syncing was faultless. ... iPhone, or delaying the purchase, it makes it more likely I'll get one ... She is a phone Luddite and continues to use a very ...
    (uk.comp.sys.mac)
  • Re: How To Branch Java
    ... I've been told that people use source control. ... > code in sync with the production code. ... interface is used in production. ... and subclass can be tested, ...
    (comp.lang.java.programmer)
  • Re: Problems with inheritance!
    ... public void meth1; ... variates from subclass to subclass.... ... When I use an Interface, I have to write duplicated code, havent't I? ... Why particularly extend should be avoided you can read article at ...
    (comp.lang.java.programmer)
  • Re: Pickling and inheritance are making me hurt
    ... > these classes by writing other modules that subclass both of them as ... > to interface with particular search engines. ... > It seems like the problem is that _saveresults is only looking inside ... Maybe this interferes with pickling your first instance? ...
    (comp.lang.python)