0

I am new to Java. I have been researching the notion of an abstract class.

I understand that abstract classes cannot be instantiated. So an abstract subclass could not be instantiated with new in the abstract base class.

However, I have been looking for a driver/main class that is abstract to study since instantiating is not allowed.

Example:

public abstract class Research{
    public static void main(String [] args){
          [Code here
    }
}

Can anyone please explain what a driver/main class for an abstract class would look like? Thank you.

  • 3
    It doesn't exist. You can't "drive" an abstract class because it just describes an interface, and doesn't provide any concrete methods to call. You must make a class that implements the abstract class, and drive that. – nobody Oct 19 '15 at 03:18

2 Answers2

1

An abstract class cannot be instantiated

public abstract class Person {
    // hi i am an abstract person
    // put your abstract methods here
}

public class Student extends Person {
    // This class provides the implementation of the abstract methods in the Person abstract class
    // implement the abstract methods of Person
}

public class Driver {
    public static void main(String[] args) {
        // Person p = new Person(); // wrong

        Student s = new Student();
    }
}

See https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html it explains in detail abstract classes in Java.

krato
  • 1,226
  • 4
  • 14
  • 30
  • So the driver class does not require an extension or implementation keyword of the Person or Student classes? – Justin_Finland Oct 19 '15 at 03:49
  • 1
    The one that should implement the abstract methods of the **abstract** class is the **real** class. Now the real class will be the object you would be using. – krato Oct 19 '15 at 03:53
  • Ohh I understand now. Thank you for the clarification, post and link. I appreciate your help. This is exactly what I was trying to grasp. – Justin_Finland Oct 19 '15 at 03:55
  • 1
    You're welcome, you can learn more of the OOP concepts in Oracle's Java docs. There are example codes there to help you understand the concepts more. – krato Oct 19 '15 at 03:58
  • Why don't you program to an astract class, Person p = new Student() ? is it not the same case as program to an interface?https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface – karlihnos May 16 '18 at 08:10
0

The word ABSTRACT should hint you that it is not real. That's why you cannot touch a real instance of that class(like interface). But asbtract class can have some method implementations(interface does not have). In order to test already implemented metods just extend that class with some stubs and test already implemented methods. Lets say

abstract class AbstractClass{
  implementedMethod(){ //some code}
  abstractMehod()
}

class ExtendedClass extends AbstractClass{
  abstractMethod(){//implementation}
  public static void main(String args[ ]) {
   //here you can drive
  }
}
Doseke
  • 861
  • 8
  • 15
  • I am beginning to understand, but would like some clarification. So, abstract drivers do not exist. Say I have an abstract class called Person and a subclass called Student. I want to have a driver class...how would I go about doing that? Please explain. Thanks! – Justin_Finland Oct 19 '15 at 03:35
  • @Justin site isn't really aimed at answering questions of that type. I suggest reading a couple of tutorials out there, maybe pick up a book in OOP? – Emz Oct 19 '15 at 03:45
  • @Emz Thanks. I understand that. I have looked into tutorials and have a Java book in front of me right now, but I cannot locate the information I am interested in understanding. – Justin_Finland Oct 19 '15 at 03:48