| Author |
Regarding abstract class
|
surya.raaj prakash
Ranch Hand
Joined: Oct 30, 2009
Posts: 76
|
|
Hi Friends,
My Question is in which scenario we can use this design?
|
 |
Lee Kian Giap
Ranch Hand
Joined: Jan 23, 2008
Posts: 210
|
|
|
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.
|
SCJP 6, SCWCD 5, SCBCD 5
|
 |
Dejan Miler
Ranch Hand
Joined: Nov 14, 2009
Posts: 56
|
|
We use abstract class when we want to force implementing class to implement abstract class methods.
|
SCJP 1.6 in progress ....
|
 |
Lorand Komaromi
Ranch Hand
Joined: Oct 08, 2009
Posts: 276
|
|
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...
|
OCJP 6 (93%)
|
 |
Dejan Miler
Ranch Hand
Joined: Nov 14, 2009
Posts: 56
|
|
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
Joined: Jan 23, 2008
Posts: 210
|
|
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.
|
 |
PrasannaKumar Sathiyanantham
Ranch Hand
Joined: Nov 12, 2009
Posts: 110
|
|
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
|
To err is human,
To forgive is not company policy
|
 |
Dejan Miler
Ranch Hand
Joined: Nov 14, 2009
Posts: 56
|
|
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
Joined: Jan 23, 2008
Posts: 210
|
|
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
Joined: Jan 23, 2008
Posts: 210
|
|
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
Joined: Nov 14, 2009
Posts: 56
|
|
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
Joined: Oct 30, 2009
Posts: 76
|
|
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
Joined: Nov 14, 2009
Posts: 56
|
|
|
I do not see any reason at all how that can brake you code
|
 |
Lee Kian Giap
Ranch Hand
Joined: Jan 23, 2008
Posts: 210
|
|
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).
|
 |
 |
|
|
subject: Regarding abstract class
|
|
|