• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Regarding abstract class

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



My Question is in which scenario we can use this design?
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you don't allow anyone to instantiate this class, but you want to have implementation code in it ... and may or may not have abstract method.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We use abstract class when we want to force implementing class to implement abstract class methods.
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dejan Miler wrote:We use abstract class when we want to force implementing class to implement abstract class methods.



Wrong, an abstract class doesn't need to have any abstract methods...
 
Dejan Miler
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that
The main purpose of creating abstract class is when we want to make that it cannot be initialized and to force
first non abstract class who extends that abstract class to implement all abstract method from that abstract class.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lorand Komaromi wrote:

Dejan Miler wrote:We use abstract class when we want to force implementing class to implement abstract class methods.



Wrong, an abstract class doesn't need to have any abstract methods...



Yes , Dejan Miler statement is not for abstract class, it is actually more appropriate for interface, all method in interface is implicitly public abstract.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we declare a class abstract for two reasons

1)to prevent creting objects
2)if the class has any abstract methods

if the class has abstract methods then the class that inherits it must implemet the methods that are declared abstract or declare itself abstract....
Hence if you want to force a sub class to implement the super class method the method must be declared abstract and the class abstract

abstract class a{

abstract void put(){}
void get(){}
}
class b extends a{
/*it must override the abstract methods put of class a but no need to override get() method*/
}

abstract classes can contain fields that are not static and final and also implemented methods

but if the abstract class contains only abstract methods then it the same as a interface
 
Dejan Miler
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are fast


Yes , Dejan Miler statement is not for abstract class, it is actually more appropriate for interface, all method in interface is implicitly public abstract.



I agree that statment is more appropriate for interface.
But we all know that we can have abstract class with methods who are all abstract.
So if all methods in abstract classes are abstract we can say that that class is very similar to interface.
And we all know that interface is 100% abstract and class can or do not need to be 100% abstract.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


we declare a class abstract for two reasons

1)to prevent creting objects
2)if the class has any abstract methods

if the class has abstract methods then the class that inherits it must implemet the methods that are declared abstract or declare itself abstract....
Hence if you want to force a sub class to implement the super class method the method must be declared abstract and the class abstract



Yes, you are right, this is the rules/specifications.

Now, the important point is, if you only have abstract method in the abstract class, you should consider using interface.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I agree that statment is more appropriate for interface.
But we all know that we can have abstract class with methods who are all abstract.
So if all methods in abstract classes are abstract we can say that that class is very similar to interface.
And we all know that interface is 100% abstract and class can or do not need to be 100% abstract.



Yes, you are right. What you mentioned is still rules/specification
It is similar and it is not similar, it will cost you in your future of design.
If you have 100% abstract method, please consider using interface, code to interface is a good practice.
And it will help you in the future, for example on multiple inheritance. Also for design pattern , because software in real world is tend to grows and tend to have mid course changes.
 
Dejan Miler
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here I had just try to defend my honor.
And of course that i will use interface rather then abstract class
 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
Thanks For Your Replies.

But one more doubt, suppose if i will add two more concrete methods(like m4() and m5())to my abstract class,will it break the clients code(who uses my code)?
 
Dejan Miler
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not see any reason at all how that can brake you code
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But one more doubt, suppose if i will add two more concrete methods(like m4() and m5())to my abstract class,will it break the clients code(who uses my code)?



To answer your question , I will assume that you are mentioning the subclass that extends your abstract class.

By adding concrete methods to your abstract class , it won't break in term of code ... but be careful of your design, all the subclasses inherited from here will have those two methods, so you have to consider carefully when changing a parent class, it will break the purpose of the subclass (means the cohesiveness or in general term its abstraction level).

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic