An abstract class cannot be instantiated. Although not required, it normally means that there is an abstract method declared in the class, too. An abstract method does not have an implementation in the class that it is defined. The intent is to have descendants of the abstract class provide implementations of the abstract methods. The abstract class and methods merely define the interface, essentially promising that descendants will also provide those methods. As an alternative to abstract classes, you can also consider using Interfaces instead. Interfaces allow more flexibility in your design in that the implementing classes do not need to have a common supertype (apart from Object). Joshua Bloch discusses the pros and cons for using one or the other in his book "Effective Java"
------------------ Junilu Lacar Sun Certified Programmer for the Java� 2 Platform
Abstraction and Polymorphism are 2 TOTALLY different concepts. Yet they go hand in hand in a way. Now that you're totally confused, lemme finish.... Abstraction is the notion of taking real-world objects and representing them as programming code, but not just any code. They are tightly represented by a class. The more you abstract the better. I have a car. My car has an engine. The engine has valves. Inside the valves are rods....etc. All these should be classes. I suck at explanations. Polymorphism is the ability to represent these objects via another representation. The ability to inherit methods and variables that have already been defined. And the ability to tell the parent members from the descendant members at runtime. Reading over what I've just said, it doesn't even make sense to me, so I'll shut up. But to answer your question, yes they are very different. Somebody tell me that was an ok explanation so I can sleep tonight.
Michael J Bruesch<br /><i>I code, therefore I am.</i>
Joined: Aug 06, 2001
An abstract example could be "transport" which would be extended by classes such as "car" "cycle" "ship". The definition of "transport" would include the names of methods common to all forms of transport but without an implementation (e.g. GetTopSpeed(), GetMaxPassengers() ) as the implementation would be different for each sub-class. Transport would be higher up the hierarchy than say "car" but you would not create a transport object, you would create a car object. In some cases it may be more appropriate to define "transport" as an interface. In both cases though you can code like this:
You can define a variable to be an abstract class or interface. It can be used to refer to objects of types that extend the abstract class or implement the interface.
Originally posted by Margaret Tan: How is abtraction different from polymorphism?
As Mike said, they are different but often go hand-in-hand. The purpose of Abstraction is to extract the "essence" of something and hide the details. For example, the icons on your desktop (assuming you're using some GUI OS like Windows). The icons are abstractions of the actual entities that exist on your system, usually an executable program. The act of clicking on the icon is an abstraction for certain OS commands required to perform a certain task. That is, when you click (or double click, as the case may be) on the icon, something will happen. You don't really care what the details of making something happen are just as long as it happens. The OS takes care of the actual details. By the same token, an abstract Java class defines an interface that guarantees certain services to be available to users of that class. Users don't care about the details, they just need to know that the capability is there. Polymorphism, which means "many forms", usually refers to the ability to treat different objects the same way. Going back to the desktop icon example, some icons may not represent executable programs: they may represent documents instead. However, the interface is still pretty much the same in that you can click/double-click on the icon and the program that is used to manipulate the document will be started with the document loaded. The essential thing is that something happens when you click on the icon. By using an abstraction (click on the icon, do something), you can use different objects (program or document) polymorphically and invoke capabilities or behavior (run program or edit document).
------------------ Junilu Lacar Sun Certified Programmer for the Java� 2 Platform [This message has been edited by JUNILU LACAR (edited October 26, 2001).] [This message has been edited by JUNILU LACAR (edited October 26, 2001).]
Joined: Sep 14, 2001
So polymorphism and abstraction works with each other then? Since you can't use the concept polymorphism without an abstract class right?
Joined: Nov 06, 2000
Margaret, the concept of polymorphism also works with interfaces. An interface has only abstract classes and public and final variables (constants)
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley