1

I have a public abstract class named EmployeeFileProcessor, which is also what I am trying to instantiate. How would I easily fix this code so that I can instantiate this?

public abstract class EmployeeFileProcessor
{
    public static void main(String[] args) throws Exception
    {   
        EmployeeFileProcessor process = new EmployeeFileProcessor();
        //Non important code here
        process.writeFullTimeEmployee(user_input1, FtCollection[0]);
        //Defined in another class (method writeFullTimeEmployee)
        process.readFulltimeEmployee(user_input1);
        //Defined in another class (method readFullTimeEmployee)
    }
}

How would I be able to use the 'process'?

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
Coolunized5
  • 35
  • 1
  • 4
  • 1
    You cannot instantiate abstract class. Period. That is how java works. If your design requires instantiatation of abstract class, then you need to change your design – Pradeep Simha Jun 20 '15 at 18:16
  • 1
    The big question is: Why do you want this class to be abstract in the first place? Perhaps you have a mistaken notion of what "abstract" means. – GrantRobertson Jun 20 '15 at 19:14

3 Answers3

3

You can't instantiate an abstract class. You have three options :

  1. Change EmployeeFileProcessor to be a non-abstract class by removing abstract from the class declaration.
  2. Instantiate an anonymous inner subclass of your class instead : EmployeeFileProcessor process = new EmployeeFileProcessor() { }; (Not recommended)
  3. Change all the methods and fields in EmployeeFileProcessor to static. This way, you no longer need to instantiate EmployeeFileProcessor. (Not recommended unless your class doesn't have state)

In other words, don't create an abstract class if you want to instantiate it.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
  • Changing an abstract class such that it is not abstract is not a way of instantiating an abstract class. The only way to get a reference to an instance of an abstract class is to extend it and then get an instance of the class that extends it. – bhspencer Jun 20 '15 at 19:13
  • 1
    Can the downvoter please leave a comment and let me know what is wrong with my answer? As I see it, I have answered the question to the point. – Chetan Kinger Jun 20 '15 at 19:20
  • I down voted your answer because 2, 3 and 4 are by your own words "Not recommended" and point 1 does not give you an instance of an abstract class. – bhspencer Jun 20 '15 at 19:24
3

You cannot directly create an instance of an abstract class. You must first define a class that extends the abstract class and then create an instance of that class.

e.g.

public ConcreteEmployeeFileProcessor extends EmployeeFileProcessor {

}

You can then create instances of ConcreteEmployeeFileProcessor but reference it using the abstract type.

EmployeeFileProcessor instance = new ConcreteEmployeeFileProcessor();
bhspencer
  • 13,086
  • 5
  • 35
  • 44
2

Abstract class is a class that you cannot instantiate by definition. It is similar to the interface, except for the ability to declare fields and actually implement functions in case they are not overriden (for Java 8 you can use default methods in the interfaces).

Anyway, if you really want to instantiate this class in a default way, you could try to do it like this:

EmployeeFileProcessor process = new EmployeeFileProcessor() {};

But it's most likely a very poor design ;-)

pnadczuk
  • 907
  • 1
  • 7
  • 12
  • *It is similar to the interface, except for the ability to declare fields and actually implement functions in case they are not overriden*. Not true. Interfaces can have concrete methods too. – Chetan Kinger Jun 20 '15 at 18:24
  • Yes. The question has been tagged as java so it's always better to mention a specific version of Java if your answer is based on it. – Chetan Kinger Jun 20 '15 at 18:31
  • Ok, changed the answer in order to be more specific. However (it's just my opinion) I can't see big advantages of implementing a method in an interface. – pnadczuk Jun 20 '15 at 18:35
  • 1
    You are entitled to your opinion. Do read [this](http://stackoverflow.com/questions/19998454/interface-with-default-methods-vs-abstract-class-in-java-8) answer. – Chetan Kinger Jun 20 '15 at 18:47
  • Thanks, quite right about single inheritance problem ;-) – pnadczuk Jun 20 '15 at 18:54