0

Question: Why can you declare a variable of an abstract class and then set that variable to null and then not be able to access any of the methods. What would be the reason behind this concept of abstract classes?

class:

package ReadWriteFile;
public abstract class GraphicObject {
       int home = 100;
       final int score = 0;

       abstract void draw();
       abstract String meMethod1();
       abstract void meMethod2();

       void meMethod3() {
           System.out.println("test");;
       }

       public static void main(String[] i){
           GraphicObject o = null;
           o.meMethod3();
       }

}

Doug Hauf
  • 3,025
  • 8
  • 46
  • 70
  • [What does it mean to "Program to interfaces"?](http://stackoverflow.com/questions/2697783/what-does-program-to-interfaces-not-implementations-mean) – Sotirios Delimanolis Feb 16 '14 at 00:58
  • See my answer to this: http://stackoverflow.com/questions/21270862/difference-between-object-creation-syntax – ajb Feb 16 '14 at 01:12

4 Answers4

7

You have to understand two diffrent concepts, variables and instances.

You could think a variable as a holder of something, so you can create a variable of an abstract type like GraphicObject.

When you do:

GraphicObject myVar = null;

you are saying, myVar is a holder of type GraphicObject and it holding nothing.

So when you do:

myVar.meMethod3();

you are telling ´no one´ to do meMethod3();

But if you do:

GraphicObject myVar = new GraphicObjectImpl();
myVar.meMethod3();

then you are telling ´someone´ to do meMethod3();

For doing that, you need to create a ´non abstract´, instantiable class which inherits (or extends) GraphicObject, in this case GraphicObjectImpl:

public class GraphicObjectImpl extends GraphicObject {
    void draw()
    {
    }
    String meMethod1()
    {
       return null;
    }
    void meMethod2()
    {

    }
}

Which also is required to implement all the abstract methods from the parent abstract class.

Ale Zalazar
  • 1,875
  • 1
  • 11
  • 14
  • So in reality there would be no reason to create an instance of a abstract class. That is to do that you would have to extend the class over like you just showed above. Even though Java lets it compile AbstractClass temp; would never really be used because it cannot be because it is an abstract class and will not compile. – Doug Hauf Feb 16 '14 at 03:11
  • The idea behind the abstract classes is being able to create multiple ¨child¨ classes with common fields/methods without having to duplicate code. – Ale Zalazar Feb 16 '14 at 22:21
  • Someone told be that if you are going to create variable that implements an interface that you should make your variables instances of that interface rather than that class. I was wondering why this would be the case? – Doug Hauf Feb 17 '14 at 01:51
  • Let's say you have a Vehicle interface and a Car class which implements that interface. You can have a variable declaration like Vehicle vehicle = createVehicle();. And the Vehicle createVehicle() method would create a new Car() for you. Then there's ExternalCar class which comes from an external library and you want to use it. So you can create MyExternalCar class, which extends ExternalCar and implements Vehicle, make createVehicle() now return a MyExternalCar instance, and keep the rest of you code working untouched. – Ale Zalazar Feb 17 '14 at 15:11
2

You can later reuse "o" and set it to some concrete implementation of a GraphicObject.

user1825464
  • 271
  • 3
  • 6
2

We can only declare a handler for the abstract class which can be used to hold objects of the derived concrete classes. This is done to facilitate runtime polymorphism.

Vivek Vermani
  • 1,934
  • 18
  • 45
2

In order to answer this question I first have to make sure you are aware of what an abstract class is. Here is how I was told to think about the concept of abstract classes when I was first learning how to program.

Lets say you are programming a zoo simulator, and because you like using good OOP class structure you decide to make some classes to represent your animals. So you make Lion, Tiger, and Bear classes (oh my) and realize that their code is reasonably the same. Therefore you decide to create an abstract animal class that can't be instantiated. Now the whole point of abstract classes is that you can't make a new instance of them like this:

Animal animal = new Animal();

You decide that this works for your simulator because you don't want to add general animals to your zoo, you want to add specific animals to your zoo and so an abstract class is the right idea. So the idea of abstract classes is: abstract classes are used when you want a general class that your specific classes extend.

Now in your scenario you did this:

GraphicObject o = null;

So why can we do this when we can't instantiate an abstract class? Because we didn't, when you set o to null you aren't creating a new object, and therefore that is valid code. Finally on your line that looks like this:

o.meMethod3();

We can see that this is valid code, meaning that it compiles fine, however I bet if you were to run it you would get a NullPointerException because you tried to do something to a variable that doesn't have a value.

So to sum up again: The reason behind the concept of abstract classes is that sometimes you want to write code that is going to be reused in a lot of other classes, but you don't want to create an instance of that class because it is supposed to be general.

BitNinja
  • 1,477
  • 1
  • 19
  • 25
  • Thank you. This makes good sense. In reality you would never make a variable of the abstract class and set it to null even though it will compile. I read somewhere that it is better to create a variable of an interface type as opposed to an instance of the class. But the class has to implement the interface. – Doug Hauf Feb 16 '14 at 03:08