Reputation: 15940
Approach of creating objects
At present,
I have the following structure
Abstract Class A{
public abstract void B();
public abstract void C();
public abstract void D();
public void E(){
sample Statements;
}
}
public class Z extends A{
public void B() {
// TODO Auto-generated method stub
}
public void C() {
// TODO Auto-generated method stub
}
public void D() {
// TODO Auto-generated method stub
}
public void X() {
Sample Statements;;
Whcih is very specific to this Class;
}
}
My Question in the above class Z, I dont want to impl I will define only method X which is very specific to
in the Same Way I will define a new Class P, which im but other 2 methods are not implemented.
Is this the right way to implement the way of Creatin suit my requirements to create an object?
Or is there any better approach to go about to create
I am worried in Class Z, since methods C & D are not be any unforeseen effects during my programing later
One More Thought I have Got
Shall I Use the factory Design Pattern to implement the Same Objects?
Upvotes: 1
Views: 129
Reputation: 5834
Move those abstract methods to a interface. And if at all someone needs those abstract methods and method X they can extend A and implement the new interface.
Upvotes: 0
Reputation: 8884
When some or all of the abstract methods in an abstract class are unimplemented in all child classes, you're almost certainly doing something wrong. Since you didn't describe what you're trying to model, it's hard to suggest specific implementation alternatives.
Ask yourself: why did you write this abstract class in the first place? Could you accomplish the same thing with a set of interfaces?
Upvotes: 0
Reputation: 3127
Hi it really depends on what your program does whether this is an appropriate structure or not. If methods B, C and D are not used, perhaps it may not be appropriate for class Z to extend the A.
So maybe think over if it makes sense
To prevent yourself from coding an error you could throw an UnsupportedOperationException(String message) on all the overriding methods.
Another idea may be to add the @Deprecated annotation to that method. This should warn you if you accidentally use that method in your code. This is a hack and abusing it though.
Upvotes: 0
Reputation: 2936
Your design might violate the Liskov Substitution Principle. Consider using an interface for X(), and/or delegation instead of inheritance.
Upvotes: 3