0

In a code base using Factory Pattern, an abstract class is instantiated as an argument of its member method. If abstract classes can't be instantiated , how is this pattern using it? Is there any advantage of doing so?

import java.io.IOException; public abstract class abstractClass { public void member_method(abstractClass t, int i) throws IOException { // Do something } } 
2
  • 5
    You can instantiate a subclass of the abstractClass Commented May 24, 2013 at 16:43
  • 1
    I do not see any instantiation here. What am I missing? Commented May 24, 2013 at 16:46

2 Answers 2

1

The code example that you've supplied doesn't actually imply any factory-like behaviors. The fact that this class is abstract isn't really that relevant either. Here is a more concrete example implementation of your example:

public abstract class MyString { private String data; public MyString(String initialValue){ data = initialValue; } public void append(MyString toAppend, int appendCount){ for(int i = 0; i < appendCount; i ++){ data = data + toAppend.data; } } public String toString(){ return data; } public abstract notRelevantMethod(); } 

For this class (which I hope looks alot like your example above) the append method, allows you to pass another instance of MyString into a method that you're calling on MyString. In my example, the append method doesn't use any of the abstract methods defined on MyString, but it could. If it did, then it is typically following the 'template' programming pattern. However I couldn't think of a good example that used templating and had a method which took an argument of the abstract type.

Sign up to request clarification or add additional context in comments.

Comments

0

Since this is not a static method you must assume that the instance passed into the member_method is not the same instance as this.

So you obviously can have two instances of this class a and b created somehow (ie. creating a concrete child class). All this method says is that you can do a.member_method(b);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.