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

anita dhar
Ranch Hand

Joined: May 24, 2007
Posts: 34
Hi .
I am veeeeeeeery confused with abstract class why we go for this use .what is the need of it ? if possible tell me sone real time example on this
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hi,

Googling for this will most likely be the best way to go as there is tons of information out there but this is a small reason.

Abstract classes tend to be used when you have a bunch of classes that can extend the same types of thing e.g.

you could have an Employee abstract class that holds the information common to all employees and then you could have a bunch of class' such as Manager or SalesPerson that extend it, altho they will have the same common data used in the Employee class they will also have there own unique data they need to store.

Abstract methods can then also be used to force the subclass to implement some sort of behaviour.

e.g. if the abstract class defines a method -

the subclass will then have to implement this method.


you should also take a look at interfaces.

Abstract Class Vs Interface

Hope some of this helps,

John
[ June 06, 2007: Message edited by: John Bartlett ]
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
The OutputStream for example is abstract.

Class and one method are abstract so that when for example FileOutputStream subclasses OutputStream it must at leat provide void write( int b );


Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output.


If the OutputStream wore an Interface then all methods needed to be implemented. Now you MUST implement void write( int b ) but can choose to implement the other methods.


SCJP1.4
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
A good example would be to study the class hierarchy of java collections API.


Rahul Bhattacharjee
LinkedIn - Blog
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi
pretty sure that the replies above this are more technical
but i think instead of you asking what is the use of abstract classes
and apart from the Classic Employee - Manager example, i would suggest you
to think of some real time situation in which you have an entity, which is more general but its derivatives are specific
think of anything say beverage -coffee , bank account - savings account
you will notice the obvious fact and that is nothing but abstract classes.

Hope it helps


SCJP, SCWCD.
|Asking Good Questions|
sudhan Sadanand
Greenhorn

Joined: Jul 25, 2006
Posts: 4
My understanding of what an abstract class is and what its used for:

As we all know everything in java (well almost everything) revolves around classes and creating objects of those classes.

Classes have their own 1.variables - to hold values and 2.Methods - to act on the variables.

When one creates an object of the class, the variables in that object gets initialized.

We do know that classes are subclassed (The exciting world of inheritance)

When a class is subclassed, the subclasses inherits those states.

Now lets consider a real life situation - lets say you create a class called Car and extend it to form various subclasses like Bmw, Ferrari...

You might define common methods of every car like(accelerate(),turn() in your Car class but

Car objects would be toooo lets say abstract and they cannot have any particular implementation of accelerate() or turn() unless its an object of a Bmw or a Ferrari.

Abstract classes are thus a solution for such situations.
By defining a Class as an Abstract class you mention those behaviours that are common to cars without actually giving their definition(Y would you, a Bmw will implement it differently from a Ferrari object).
Though you can also have concrete methods(meaning methods that are not abstract in an abstract class too)


◄ Sudhan ►
anita dhar
Ranch Hand

Joined: May 24, 2007
Posts: 34
like we are having a abstarct class
inside that we are having private methods
can we call a privte method from this class
Simon Baker
Ranch Hand

Joined: Sep 09, 2004
Posts: 57
Hi Anita,

Private members, including methods, of a class are only available to be accessed from within that class. The Java Tutorial has an explanation of this.

Cheers,
Simon
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
anita dhar have have a look at the access level 'Protected', this is commonly used in the abstract class model.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract class