• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Abstract Class required?  RSS feed

 
Kaustubh Sharma
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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,
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And at the Template Method pattern.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!