java array of objects and inheritance

16,498

Solution 1

AbstractClass array[]=new AbstractClass[1];
array[0]= new Subclass(); // is this even allowed?

Yes, that's fine, but it means that when you later go to use it, you only have access to what's defined in AbstractClass (barring using a cast, but you want to avoid using casts wherever you can, and there's no need for one here).

The only real reason for making the array entries of type AbstractClass would be if you only want to interact with the members defined in that class. So for instance, in this case, you'd probably want to have the getDataToExport defined as an abstract method in the abstract class:

public abstract class AbstractClass {
    public abstract int getDataToExport();
}

You might also consider looking at having an interface rather than an abstract class. Since a class can only derive from one base class, but can implement as many interfaces as it likes, unless there's a large body of common implementation that you'd be putting in the abstract base class, you're better off with an interface — because it doesn't put unnecessary constraints on the implementations of that interface. In fact, you're almost always better off with an interface; you can always also have an abstract base if you want.

So for instance:

public class MainClass {

    public static void main() {

        NiftyThingy array[]=new NiftyThingy[1];
        array[0]= new NiftyThingyImplementation();
        System.out.println(array[0].getDataToExport());

    }

}

where

public interface NiftyThingy {

    public int getDataToExport();

}

and

public class NiftyThingyImplementation implements NiftyThingy {

    public int getDataToExport() {
        return /* ... the data ... */;
    }

}

Solution 2

You must declare getDataToExport() as an abstract method in AbstractClass.

Share:
16,498
Luka
Author by

Luka

Updated on June 04, 2022

Comments

  • Luka
    Luka almost 2 years

    I have 3 classes, MainClass with main method, one abstract class named AbstractClass and Subclass which is to be extended with AbstractClass.

    The array of objects is created in main method from type AbstractClass containing 1 member. Then I initialize that 1 element of array as type Subclass( is this ok?). The problem is I can't get to the getDataToExport() method of created object ( array[0] ) . I suspect the problem occurs because the array is the AbstractClass type... And the question: is this even possible to accomplish? What I'm trying to do is use an array of type AbstractClass and fill it with objects made from different subclasses( in this code is just one->Subclass ) extended with AbstractClass but i can't get to the methods of that subclasses.

    main class with main method

    public class MainClass {
    
        public static void main() {
    
            AbstractClass array[]=new AbstractClass[1];
            array[0]= new Subclass(); // is this even allowed?
            System.out.println(array[0].getDataToExport()); // Problem!
    
        }
    
    }
    

    abstract class

    public abstract class AbstractClass {
    
    }
    

    Subclass which extends AbstractClass

    public class Subclass extends AbstractClass {
    
        private int dataToExport;
    
        public Subclass(){
            this.dataToExport=2;
        }
    
        public int getDataToExport() {
            return dataToExport;
        }   
    
    }