What Is an Abstract Class?

     

You cannot instantiate an abstract class. That is, you cannot make an object of it. You can't write new SecondAbstractClass( ). Ever. This is sensible , as it is the way the world works. The world has things in it, like balls and books and rocks. The world also has abstract concepts that help us organize actual things, such as rocks and people, and to organize other concepts. The economy is a good example. There is no such thing as "the economy." Anywhere. It doesn't exist. We talk about it enough. "Blah blah blah how's the economy . Ah it's horrible . Ah well what're you gunna do ." Because "the economy" is a useful placeholder for a collection of other things ”some of which are physical objects, and some of which are other concepts. You can't touch the economy, or hang it out the window, or kick it. It doesn't exist. You can't say new Economy() . You can only allocate 20 pounds British Sterling to Jill, and transfer 1,000 shares of Sun Microsystems stock to Phil, and collect $387.65 interest on your specific mutual fund run by a specific company containing specific stocks of specific companies that exist in the world. Those are the actual things of the economy. And the interest rate and whether the NASDAQ is generally bullish or bearish, these are abstract. So it is well and just that abstract classes should support both a little implementation (thing-ness) and a little idea (abstract-ness). Or neither . Or one. Or both.

That's why SimplestAbstractClass, SecondAbstractClass and ThirdAbstractClass above all make sense as possible representations of an abstract class within the Java programming language.

Some Guidelines About Abstract Classes

  • When you want to declare a method to indicate something that should be done, but you want to defer the implementation to a subclass, you use an abstract class. That's what they're for. Any method that you want to let a subclass implement you declare using the abstract modifier. We can call this an abstract method.

  • You can mix implementing methods and abstract methods in an abstract class. However, a class must be declared abstract itself if it contains even one abstract method.

  • You cannot instantiate an abstract class. That is, you cannot make an object of it. You can't write new SecondAbstractClass( ). Ever.

  • A class cannot be both abstract and final at the same time. Think about it: abstract means that you must extend the class and implement it somewhere, and final means you are totally not allowed to ever extend this class. They are mutually exclusive. Note that when I write, "you must extend it ," I don't really mean it. That is, you can create an abstract class in your app and never have any other class extend it. It just wouldn't do anything then.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net