• 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

Why Abstract Class required?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi....

Why we need abstract class ??? we can also perform all the task from a simple class also...Looking for a solid answer ....

Enjoy.........

kaustubh
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Abstract classes are used when you want to generalize the working of a class and let some specific
methods be implemented by the sub-classes. This way, the code is not duplicated as some general behavior
is defined in the abstract class and the sub-class only needs to implement the methods specific to its functionality.
Code duplication is avoided. Promotes good object oriented principles.

Have you done SCJP or preparing for it?

Best Regards,
 
Kaustubh Sharma
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohk but this functionality can be achieved by simple class also then why we need a special class for it.... any more good reason for it...??? thanks for your interest.

I done with my scjp...


Kaustubh
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Abstract classes let you define some behaviors; they force your subclasses to provide others.

For example, if you have an application framework, an abstract class may provide default services such as event and message handling. Those services
allow your application to plug in to your application framework. However, there is some application-specific functionality that only your application can perform.
Such functionality might include startup and shutdown tasks, which are often application-dependent. So instead of trying to define that behavior itself, the abstract
base class can declare abstract shutdown and startup methods. The base class knows that it needs those methods, but an abstract class lets your class admit that
it doesn't know how to perform those actions; it only knows that it must initiate the actions. When it is time to start up, the abstract class can call the startup method.
When the base class calls this method, Java calls the method defined by the child class.

Well you can't have incomplete implementation with a simple class. In a simple class you have to provide all the concrete implementations. Where as in abstract
class, you can have some concrete and some incomplete implementations.

I will highly recommend you to have a look at Strategy Pattern.

Best Regards,
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And at the Template Method pattern.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I may quote some from 'Effective Java'by Joshua Bloch (Excellent book btw)

You should generally prefer interfaces to Abstract classes for a number of reasons but a few of the advantages of abstract classes over interfaces are as follows:

- It is far easier to evolve an abstract class than an interface
- Once an interface is released and widely implemented, it is almost impossible to change

One common use of absract classes is to provide an abstract skeletal implementaion class to go with nontrivial interfaces that you are exporting.

The interface defines the type but the skeletal implementaion (abstract class) takes all the work out of implementing it.

An example of this is the Collections Framework where abstract classes exist to go along with each main collection interface
- AbstractCollection
-AbstractSet
-AbstractList
-AbsractMap

In short do not use absract classes to serve as Type defenitions but rather use them to provide implementation assistance.

Hope this helps
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic