aspose file tools*
The moose likes Beginning Java and the fly likes abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "abstract class" Watch "abstract class" New topic
Author

abstract class

Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
Hello
What is Abstact in java. I read in the book "Thinking in java 2" but still have no idea.
Thanks
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

For a start abstract is a OO concept, it isn't just a "java" thing. Have you read anything else? Here are a couple of links that I quickly found: http://www.j3ltd.com/articles/oo.htm http://java.sun.com/docs/books/tutorial/java/javaOO/abstract.html

Please ignore post, I have no idea what I am talking about.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4704
    
    7

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


Junilu - [How to Ask Questions] [How to Answer Questions]
Margaret Tan
Greenhorn

Joined: Sep 14, 2001
Posts: 20
How is abtraction different from polymorphism?
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
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
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
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.

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4704
    
    7

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).]
Margaret Tan
Greenhorn

Joined: Sep 14, 2001
Posts: 20
So polymorphism and abstraction works with each other then? Since you can't use the concept polymorphism without an abstract class right?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Margaret, the concept of polymorphism also works with interfaces. An interface has only abstract classes and public and final variables (constants)

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4704
    
    7

JavaWorld has a number of good articles that discuss polymorphism and abstraction
http://www.idg.net/english/index_searchjw.html?as_q=polymorphism&sh=0&nh=10&as_sitesearch=javaworld.com&as_eq=p&rv=r&sid=jw
------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract class