0

Long story short, I cannot figure out why this code doesn't work.

import java.util.*;

class sup{
  int a;

  sup(){
    a = 1;
  }
}

class subB extends sup{
  int b;

  subB(){
    b = 2;
  }
}

class subC extends sup{
  int c;

  subC(){
    c = 3;
  }
}

public class runMain{
  public static void main(String args[]){
    List<Object> classes = new ArrayList<Object>();

    classes.add(new sup());
    classes.add(new subB());
    classes.add(new subC());

    System.out.println(classes.get(0).a); //should print 1
    System.out.println(classes.get(1).b); //should print 2
    System.out.println(classes.get(2).c); //should print 3

  }
}

As stated in the comment, I am wanting the last 3 lines to print the values of a, b and c in the different classes. When instead having e.g. System.out.println(classes.get(0)); it prints sup@6d06d69c, which I assume is just the class name "@" the memory location, so everything else in the program seems to be working as intended.

Dant
  • 92
  • 6
  • I know why it's printing sup@6d06d69c as I stated in the text, that wasn't my question. I'm asking how to print/access a particular variable that is part of the class. – Dant Jul 12 '17 at 18:02
  • My bad. Here's a dupe for what you're actually asking: https://stackoverflow.com/questions/4512312/access-subclass-fields-from-a-base-class-in-java You'll need to cast the value to its corresponding subclass. – Sotirios Delimanolis Jul 12 '17 at 18:03
  • That solves the problem if the object is not stored inside an ArrayList, which I tried before and worked just fine. Its just that referencing the variable of an object in an ArrayList seems not to work as would be intuitive, at least to me, in the form I have put in the original block of code. – Dant Jul 12 '17 at 18:10
  • Where it's stored does not matter. You can cast what `get` returns the same way you'd cast a variable. `((sup)classes.get(0)).a`. Or just assign its return value to a variable first and cast that. – Sotirios Delimanolis Jul 12 '17 at 18:12
  • Ah, my mistake, I was reading the wrong part of the solution. I suppose since i declare my ArrayList of type Object it needs to be cast before access. Is there a similar way to set variables and run methods within this Object in the ArrayList? – Dant Jul 12 '17 at 18:19

1 Answers1

0

Benefits of Inheritance include code re-use, and method overloading - neither of which your code is exploiting. In your case it may either be:

  1. Re-use: of the super-class variable a, in which case the sub-classes would not have their own variables b and c, but rather assign their respective values to the super-class declaration of a, assuming it is either public or protected. Better coding style would include a shared getter method in the super class.

  2. Method overload: Each of the three classes would have a commonly-named method that returns their respective values.

In this example, the three classes involved in the inheritance share a variable, and a getter:

import java.util.ArrayList;

public class MyClass{

static class sup{
  protected int a;

  sup(){
    a = 1;
  }

  public int a() {
      return a;
  }
}

static class subB extends sup{


  subB(){
    a = 2;
  }
}

static class subC extends sup{


  subC(){
    a = 3;
  }
}

public static void main(String[] args) {

    ArrayList<sup> classes = new ArrayList<sup>();

    classes.add(new sup());
    classes.add(new subB());
    classes.add(new subC());

    System.out.println(classes.get(0).a());
    System.out.println(classes.get(1).a());
    System.out.println(classes.get(2).a());
}

}

JohnG79
  • 1,485
  • 1
  • 17
  • 23
  • Sorry, I suppose my code here has oversimplified my problem. I intend to have the subclasses include a lot of different methods and variables specific to only that subclass, so defining everything in the superclass would defeat the purpose of my creation of the subclasses for organisation in the first place. – Dant Jul 12 '17 at 18:26
  • If you are not sharing code or variables, or using static binding then why are you using inheritance? – JohnG79 Jul 12 '17 at 18:35
  • I will be sharing code, the superclass will still have many methods being utilised by subclass objects, but further specialised methods will be in each of the subclasses. – Dant Jul 12 '17 at 18:38