• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abstract class

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
What is Abstact in java. I read in the book "Thinking in java 2" but still have no idea.
Thanks
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is abtraction different from polymorphism?
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So polymorphism and abstraction works with each other then? Since you can't use the concept polymorphism without an abstract class right?
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Margaret, the concept of polymorphism also works with interfaces. An interface has only abstract classes and public and final variables (constants)

Bosun
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic