• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

abstract class

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good example would be to study the class hierarchy of java collections API.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
anita dhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
like we are having a abstarct class
inside that we are having private methods
can we call a privte method from this class
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anita dhar have have a look at the access level 'Protected', this is commonly used in the abstract class model.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic